简体   繁体   English

javax.servlet.jsp.JspTagException:不知道如何迭代 <forEach> 中提供的“项目”

[英]javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach&gt

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.

相关问题 不知道如何迭代<c:forEach>中提供的“items” - Don't know how to iterate over supplied “items” in <c:forEach> jstl解析错误:javax.servlet.jsp.JspTagException:无法识别的对象作为“ xml”属性提供以进行解析 - jstl parse error: javax.servlet.jsp.JspTagException: Unrecognized object supplied as 'xml' attribute to parse JSTL消息:不知道如何使用forEach迭代提供的“items” - JSTL message: Don't know how to iterate over supplied “items” with forEach javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在代码“ textv”下找不到语言环境“ pl_PL”的消息 - javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'textv' for locale 'pl_PL' SPRING:javax.servlet.jsp.JspTagException:在区域设置“en_US”的代码“spittr.welcome”下找不到消息 - SPRING: javax.servlet.jsp.JspTagException: No message found under code 'spittr.welcome' for locale 'en_US' javax.servlet.jsp.JspTagException:无效的JSP文件%2e%2e /%2e%2e /%2e%2e /%2e%2e / system / autoexec.ncf - javax.servlet.jsp.JspTagException: Invalid JSP file %2e%2e/%2e%2e/%2e%2e/%2e%2e/system/autoexec.ncf 如何在jsp / servlet中进行gt树视图 - How to gt tree view in jsp/servlet <c:foreach jsp iterate over list - <c:foreach jsp iterate over list 如何在JSP中遍历Hashtable - How to iterate over Hashtable in JSP Jsp文件无法迭代servlet中的复杂元素 - Jsp file fails to iterate over complex elements from a servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM