[英]javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
What I am want to do is to add a record to a table and then to display that newly added record in a jsp page.我想要做的是将记录添加到表中,然后在 jsp 页面中显示新添加的记录。
Here is what I tried:这是我尝试过的:
Controller: Controller:
@Controller
public class UserController {
@Autowired
UserRepository userRepo;
@RequestMapping("/adduser")
public String userform(Model m){
m.addAttribute("command", new Users());
return "adduser";
}
@RequestMapping("/saveuser")
public String save(@ModelAttribute("user") Users user,Model model){
userRepo.save(user);
model.addAttribute("user", user);
return "forward:/user";
}
@RequestMapping("/user")
public String user(Model model) {
return "user";
}
And the jsp page:和 jsp 页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css"
href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
<c:url value="/css/main.css" var="jstlCss" />
<link href="${jstlCss}" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="starter-template">
<h1>Product List</h1>
<table
class="table table-striped table-hover table-condensed table-bordered">
<tr>
<th>Id</th>
<th>Color</th>
<th>Gender</th>
<th>Category</th>
</tr>
<c:forEach var="u" items="${user}">
<tr>
<td>${u.userid}</td>
<td>${u.username}</td>
<td>${u.userphone}</td>
<td>${u.useremail}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<script type="text/javascript"
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
Upon running the code the record is getting added successfully but It is not getting displayed instead I am getting the error mentioned in the title.运行代码后,记录已成功添加,但没有显示,而是出现标题中提到的错误。
What to do to make it work?怎么做才能让它发挥作用?
@RequestMapping("/user")
public List<User> user(Model model) {
...
return ...;
}
${user}
is the result of that method user
. ${user}
是该方法user
的结果。
<c:forEach var="u" items="${user}">
The forEach
tag requires as items
something Iterable
. forEach
标签需要Iterable
作为items
。
In fact the name should have been plural: users
.事实上,这个名字应该是复数形式: users
。
You need something like你需要类似的东西
public class User {
int userid;
String username;
String userphone;
String useremail;
}
<td>${u.userid}</td>
<td>${u.username}</td>
<td>${u.userphone}</td>
<td>${u.useremail}</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.