如何将列表从控制器传递到jsp?

hee*_*esu 2 spring jsp

大家好,我对Spring和JSP还是很陌生,现在我正在实现一个带有Spring框架的公告板。

为了显示有关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";
}
Run Code Online (Sandbox Code Playgroud)

接下来,此代码用于从控制器中检索列表

<%@ page import com.heesu.third.BulletinBoardList, java.util.List %>
<% BulletinBoardList list = ${list} %>
Run Code Online (Sandbox Code Playgroud)

但这永远都行不通...

str*_*ash 5

我已经在jsp中使用jstl完成了这样的事情。看一下我的代码。在这里,我的列表是“ inf”,并且我的对象具有不同的字段,但是我认为您将设法适应它:

<%@ 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>
Run Code Online (Sandbox Code Playgroud)

这是控制器的特定部分:

@RequestMapping(value = "/search", method = RequestMethod.POST)
    public String handleRequest(@RequestParam String search,
            ModelMap model){
        List<UserInfo> inf = searchServ.listPeople(search);
        model.put("inf", inf);
        return "info";
    }
Run Code Online (Sandbox Code Playgroud)

你的事情很相似。希望对您有所帮助。只是要添加-我正在使用引导程序来设置图形用户界面的样式。