使用Spring显示JSP上的值列表 [英] Display list of values on JSP using Spring
问题描述
我想在jsp视图中显示我的值列表,但我无法做到这一点。
I would like to display my List of values in my jsp view, but i am not able to do this.
下面是我的控制器类,它只是将List添加到 ModelAndView
地图,而不是重定向到我的 index.jsp
page。
Here is my controller class below, which is only add List to the ModelAndView
map and than it redirects to my index.jsp
page.
EmployeeController
@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)
打印Kontroler EmployeeController
进入Tomcat Log,但 index.jsp
页面为空。
I am able to access the controller, because every time I hit "Zaměstnanci"
, the System.out.println("Kontroler EmployeeController")
prints "Kontroler EmployeeController"
in to Tomcat Log, but the index.jsp
page is blank.
请有人给我一个建议吗?
Please, can someone give me an advice?
推荐答案
As您正在填充 ModelAndView
返回ModelAndView本身而不是 map.getViewName()
仅返回没有数据的名称的名称在docs中声明:
As you are populating ModelAndView
return ModelAndView itself and not map.getViewName()
which return only name of the name without data as stated in docs:
public String getViewName()通过ViewResolver返回
DispatcherServlet要解析的视图名称,或者如果我们使用View
对象,则返回null。
public String getViewName() Return the view name to be resolved by the DispatcherServlet via a ViewResolver, or null if we are using a View object.
如下所示:
@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标签<%索引页面上的@ taglib前缀=curi =http://java.sun.com/jsp/jstl/core%>
,您给列表的变量名称是列表 所以迭代list而不是listEmployee,如下所示:
Secondly, you are missing jstl tag <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
on your index page and variable name you given to list is "lists" so iterate over "lists" rather than "listEmployee", as follows:
<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>
这篇关于使用Spring显示JSP上的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!