Java 使用 Spring 在 JSP 上显示值列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42054829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display list of values on JSP using Spring
提问by Stepan Mocanu
I would like to display my List of values in my jsp view, but i am not able to do this.
我想在我的 jsp 视图中显示我的值列表,但我不能这样做。
Here is my controller class below, which is only add List to the ModelAndView
map and than it redirects to my index.jsp
page.
下面是我的控制器类,它只是将 List 添加到ModelAndView
地图,然后重定向到我的index.jsp
页面。
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
索引.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>
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.
我能够访问控制器,因为每次我点击 时"Zaměstnanci"
,都会System.out.println("Kontroler EmployeeController")
打印"Kontroler EmployeeController"
到 Tomcat 日志,但index.jsp
页面是空白的。
Please, can someone give me an advice?
拜托,有人可以给我建议吗?
采纳答案by Arpit Aggarwal
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:
由于您正在填充ModelAndView
返回 ModelAndView 本身,而不是map.getViewName()
仅返回名称的名称而没有文档中所述的数据:
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.
public String getViewName() 返回要由 DispatcherServlet 通过 ViewResolver 解析的视图名称,如果我们使用的是 View 对象,则返回 null。
as follows:
如下:
@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;
}
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:
其次,您<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
在索引页面上缺少 jstl 标记 ,并且您提供给 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>
Also, make sure you have JSTLdependency in your classpath:
另外,请确保您的类路径中有JSTL依赖项:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
回答by Sergii Bishyr
Just add Model
to your controller method params and then add attribute to this model.
只需添加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";
}
One more way to do this, is to return ModelAndView
instead of String
另一种方法是返回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;
}
Just choose which way is better for you.
只需选择哪种方式更适合您。
And also change listEmployee
in your index page to lists
as in the ModelAndView
you have an attribute as lists
not listEmployee`.
并且还将listEmployee
您的索引页面更改lists
为ModelAndView
您的属性lists
不是 listEmployee`。
回答by Barath
Change the key name and try it : listEmployee --> lists as you are setting it as map.addObject("lists", list);
更改密钥名称并尝试: listEmployee --> lists as you are setting it as map.addObject("lists", list);
<ul>
<c:forEach var="listValue" items="${lists}">
<li>${listValue}</li>
</c:forEach>
</ul>