如何将List从控制器传递给jsp?

时间:2017-05-14 04:42:40

标签: spring jsp

嗨,大家好我对spring和jsp很新, 现在我正在实施一个带弹簧框架的公告板。

为了显示关于jsp的文章列表,我从DB获得了一些信息并将其保存到List。

我想要做的是......将此列表带到jsp并保存。 所以,我尝试了下面的代码。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    /*
    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );
    */
    List<BulletinBoardList> result = sqlSession.selectList("board.getList");

    model.addAttribute("list", result);




    return "home";
}

接下来,此代码用于从控制器

中检索List
<%@ page import com.heesu.third.BulletinBoardList, java.util.List %>
<% BulletinBoardList list = ${list} %>

但它永远不会有效......

1 个答案:

答案 0 :(得分:4)

我在jsp中使用jstl做过这样的事情。看看我的代码。这里我的列表是“inf”,我的对象有不同的字段,但我认为你会设法适应它:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private ClipboardManager myClipboard;
    private ClipData myClip;
    private Context context;




    public List<CardItemModel> cardItems;

    public RecyclerAdapter(List<CardItemModel> cardItems){
        this.cardItems = cardItems;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView copyButton;
        ImageView shareButton;
        ToggleButton favButton;


        TextView title;
        TextView content;
        public ViewHolder(View itemView) {
            super(itemView);
            this.title = (TextView)itemView.findViewById(R.id.card_title);
            this.content = (TextView)itemView.findViewById(R.id.card_content);
            this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
            this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);
            this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);
            favButton.setChecked(false);
            favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_launcher));


        }
    }



    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.title.setText(cardItems.get(position).title);
        holder.content.setText(cardItems.get(position).content);
        holder.copyButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){


                myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


                myClip = ClipData.newPlainText("label", holder.content.getText().toString());
                myClipboard.setPrimaryClip(myClip);
                Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

            }
        });
        holder.shareButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
                v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
            }
        });

        holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                if (isChecked)
                    favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(),R.mipmap.ic_launcher));
                else
                    favButton.setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.mipmap.ic_cart));
            }
        });
    }

    @Override
    public int getItemCount() {
        return cardItems.size();
    }
}

以下是控制器的特定部分:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Real time info</title>
<link href="webjars/bootstrap/3.3.6/css/bootstrap.min.css"
                    rel="stylesheet">
</head>
    <body>
    <div class="container">
        <table class="table table-striped">
            <caption><h3>Result (People):</h3></caption>
            <thead>
                <tr class="tr tr-success">
                    <td>Id</td>
                    <td>Name</td>
                    <td>PIN</td>
                </tr>   
            </thead>
            <tbody>
                <c:forEach items="${inf}" var="temp">
                    <tr>
                        <td>${temp.id}</td>
                        <td>${temp.name}</td>
                        <td>${temp.pin}</td>
                        <td>
                            <a class="btn btn-info" href="/update-person?id=${temp.id}">Update</a>
                            <a class="btn btn-danger" onclick="return confirm('Are you sure you want to delete?')" href="/delete-person?id=${temp.id}">Delete</a>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>
    <script src="webjars/jquery/2.2.4/jquery.min.js"></script>
    <script src="webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

你的事情非常相似。我希望这会对你有所帮助。只是添加 - 我正在使用bootstrap来设置我的图形用户界面。