Don't know how to iterate over supplied "items" in <forEach>问题的解决方法

来源:互联网 发布:锐捷网络2016财报 编辑:程序博客网 时间:2024/05/16 08:01

曾经碰到如下错误:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


后来经过检查代码,发现是在JSP文件中,JSTL的标签  <c:forEach> 里的items属性被赋予了一个无法遍历的对象。下面是重现此问题的代码:


User.java

package zc.csdn;import java.util.List;import java.util.ArrayList;import java.io.Serializable;public class User implements Serializable{/** * 序列化ID */private static final long serialVersionUID = 775564552573359116L;/** * 主键ID */private Integer id;/** * 用户名 */private String userName;/** * 书单 */private List<Book> books = new ArrayList<Book>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public List<Book> getBooks() {return books;}public void setBooks(List<Book> books) {this.books = books;}}

Book.java

package zc.csdn;import java.io.Serializable;public class Book implements Serializable {/** * 序列化ID */private static final long serialVersionUID = -5329749254581235038L;/** * 主键ID */private Integer id;/** * 书名 */private String bookName;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}}

index.jsp

<%@ page language="java" import="java.util.*,zc.csdn.*" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";User user = new User();user.setId(1);user.setUserName("lisi");Book book1 = new Book();book1.setId(1);book1.setBookName("孙子兵法");Book book2 = new Book();book2.setId(2);book2.setBookName("百万英镑");List<Book> books = new ArrayList<Book>();books.add(book1);books.add(book2);user.setBooks(books);request.setAttribute("user", user);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting pagesfsdf</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    用户${user.userName}的书单。 <br>    <table border="1">    <tr>    <th>书名</th>    </tr>    <c:forEach items="${user}" var="item">    <tr>    <td>${item.bookName}</td>    </tr>    </c:forEach>    </table>  </body></html>

由于index.jsp里的 <c:forEach items="${user}" var="item">   里的 items 被赋予了 user 这个无法遍历的对象,导致出现文章一开始提到的错误。

如果要改正这个错误,只需把那一行改成    <c:forEach items="${user.books}" var="item">   即可。由于 user.book  是一个 List ,所以 <c:forEach> 可以正常遍历它。






0 0
原创粉丝点击