【问题标题】:Display list of values on JSP using Spring使用 Spring 在 JSP 上显示值列表
【发布时间】:2017-02-05 17:04:53
【问题描述】:

我想在我的 jsp 视图中显示我的值列表,但我无法做到这一点。

下面是我的控制器类,它只是将 List 添加到 ModelAndView 映射,然后重定向到我的 index.jsp 页面。

员工控制器

@Controller
public class EmployeeController {

@RequestMapping(value={"/employee"}, method = RequestMethod.GET)
public String listEmployee(){    
    System.out.println("Kontroler EmployeeController");
    LinkedList<String> list = getList();
    ModelAndView map = new ModelAndView("index");
    map.addObject("lists", list);

    return map.getViewName();
}

private LinkedList<String> getList(){
    LinkedList<String> list = new LinkedList<>();

    list.add("Item 1");
    list.add("Item 2");
    list.add("Item 3");

    return list;
}

}

index.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Welcome to Spring Web MVC project</title>
</head>

<body>
    <h1>Index page</h1>
    <h1>${msg}</h1>
    <a href="/MavenHello/employee">Zaměstnanci</a>
</body>
    <c:if test="${not empty listEmployee}">

    <ul>
        <c:forEach var="listValue" items="${listEmployee}">
            <li>${listValue}</li>
        </c:forEach>
    </ul>

</c:if>

我能够访问控制器,因为每次我点击"Zaměstnanci"System.out.println("Kontroler EmployeeController") 都会在 Tomcat 日志中打印"Kontroler EmployeeController",但index.jsp 页面是空白的。

请问有人可以给我建议吗?

【问题讨论】:

    标签: java spring jsp spring-mvc modelandview


    【解决方案1】:

    当您填充 ModelAndView 时,返回 ModelAndView 本身而不是 map.getViewName(),它只返回名称的名称而没有文档中所述的数据:

    public String getViewName() 返回待解析的视图名称 DispatcherServlet 通过 ViewResolver,如果我们使用 View,则为 null 对象。

    如下:

    @RequestMapping(value = { "/employee" }, method = RequestMethod.GET)
    public ModelAndView listEmployee() {
        System.out.println("Kontroler EmployeeController");
        LinkedList<String> list = getList();
        ModelAndView map = new ModelAndView("index");
        map.addObject("lists", list);
    
        return map;
    }
    

    其次,您的索引页面上缺少 jstl 标记 &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;,并且您为 list 提供的变量名称是“lists”,因此迭代“lists”而不是“listEmployee”,如下所示:

    <html>
    <head>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Welcome to Spring Web MVC project</title>
    </head>
    
    <body>
        <h1>Index page</h1>
    </body>
    
    <c:if test="${not empty lists}">
        <c:forEach items="${lists}" var="lists">
           ${lists}
    </c:forEach>
    </c:if>
    

    另外,请确保您的类路径中有 JSTL 依赖项:

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    

    【讨论】:

    • 我更新了所有文件,如您上面的帖子所示,现在一切正常。谢谢你:)
    • 只是想说这个答案在 3 年后帮助了我。谢谢。
    【解决方案2】:

    只需将Model 添加到您的控制器方法参数,然后将属性添加到此模型。

    RequestMapping(value={"/employee"}, method = RequestMethod.GET)
    public String listEmployee(Model model){    
        System.out.println("Kontroler EmployeeController");
        LinkedList<String> list = getList();
        model.addAttribute("lists", list);
    
        return "index";
    }
    

    另一种方法是返回 ModelAndView 而不是 String

    @RequestMapping(value={"/employee"}, method = RequestMethod.GET)
    public ModelAndView listEmployee(){    
        System.out.println("Kontroler EmployeeController");
        LinkedList<String> list = getList();
        ModelAndView map = new ModelAndView("index");
        map.addObject("lists", list);
    
        return map;
    }
    

    只需选择更适合您的方式。

    并且还将索引页面中的listEmployee 更改为lists,就像在ModelAndView 中一样,您的属性为lists 而不是listEmployee`。

    【讨论】:

    • 好吧,在第一种情况下,我在 retun "index" 上收到错误 -> 字符串无法转换为 ModelAndView,在第二种情况下,我在 url /MavenHello/WEB-INF 上收到错误 404 /jsp/employee.jsp,现在我比以前更困惑了:D
    • @StepanMocanu 抱歉,我打错了。在第一种情况下,您应该让我们的方法返回 String。我已经把它改成了答案
    【解决方案3】:

    改键名试试看:listEmployee --&gt; lists as you are setting it as map.addObject("lists", list);

    <ul>
        <c:forEach var="listValue" items="${lists}">
            <li>${listValue}</li>
        </c:forEach>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多