使用c:foreach迭代包含Bean对象的ArrayList不起作用

时间:2014-08-18 07:11:44

标签: java jsp arraylist jstl

我在JSP中迭代ArrayList,其中包含Bean的对象。我想使用getter()从该对象中提取信息.ProductID是我感兴趣的属性之一。 我为此目的使用JSTL。代码如下:

<c:forEach items="repo" var="element">
 <jsp:useBean id="element"  scope="page" class="com.ResponseBean"/> 
Product:<jsp:getProperty property="ProductID" name="element"/>
</c:forEach>

其中repo是ArrayList,其中包含ResponseBean的对象。

List<ResponseBean> repo=new ArrayList<ResponseBean>(10);

ResponseBean代码如下:

public class ResponseBean implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String ProductID;
public String getProductID() {
    return ProductID;
}
public void setProductID(String productID) {
    ProductID = productID;
}

但我跟随例外

HTTP Status 500 - Cannot find any information on property 'ProductID' in a bean of type 'com.ResponseBean'

type Exception report

message Cannot find any information on property 'ProductID' in a bean of type 'com.ResponseBean'

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Cannot find any information on property 'ProductID' in a bean of type 'com.ResponseBean'
org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibrary.java:824)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1081)
org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1125)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1784)
org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1538)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
org.apache.jasper.compiler.Generator.generate(Generator.java:3517)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.
Apache Tomcat/7.0.52

哪里出错了?

2 个答案:

答案 0 :(得分:3)

您不需要这样做,您可以简单地使用EL

<c:forEach items="repo" var="element">
     Product: ${element.productID}
</c:forEach>

答案 1 :(得分:0)

你可以这样使用。

<c:forEach items="repo" var="element">
    <c:out value="${element.productID}">
</c:forEach>