嵌套c:forest in jstl

时间:2011-03-06 04:18:09

标签: java jsp jstl

我正在研究JSTL以从一个bean中检索值。它包含一个bean,这个bean包含另一个bean。 样本豆看起来像这样

Page.java

public class Page {
    private List<Column> columnList;
    private String emailAddr;
    public List<Column> getColumnList() {
        return columnList;
    }
    public void setColumnList(List<Column> columnList) {
        this.columnList = columnList;
    }
    public String getEmailAddr() {
        return emailAddr;
    }
    public void setEmailAddr(String emailAddr) {
        this.emailAddr = emailAddr;
    }
}

Column.java

public class Column {
    private List<MyBean> columnList;
    public String getDispCatg(){
        return dispCatg;
    }

    public void setDispCatg(String dispCatg) {
        this.dispCatg = dispCatg;
    }

    private String dispCatg;
    public List<MyBean> getColumnList() {
        return columnList;
    }

    public void setColumnList(List<MyBean> columnList) {
        this.columnList = columnList;
    }

}

MyBean.java

public class MyBean {
    private static int i=0;
    private String value;
    public String getValue() {
        return String.valueOf(++i);
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getDescription() {
        return "FirstCheckBox"+i;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    private String description;
    public MyBean() {
        // TODO Auto-generated constructor stub
    }

}

我需要访问MyBean值。我怎样才能找回来? 我尝试了以下方式...但是Jsp没有编译。

<c:forEach items="${dataBean.columnList}" var="column1" varStatus="rowStatus">
        <c:forEach items="${column1}" var="column2" varStatus="rowCount">
            <c:forEach items="${column2}" var="bean" varStatus="rowCount">
                <div>hello 1..2..3</div>
            </c:forEach>
        </c:forEach>
    </c:forEach>

完整的异常描述 java.lang.NoSuchFieldError:deferredExpression     org.apache.taglibs.standard.tag.common.core.ForEachSupport.release(ForEachSupport.java:178)     org.apache.jasper.runtime.TagHandlerPool.release(TagHandlerPool.java:166)     org.apache.jsp.BeanLoad_jsp._jspDestroy(BeanLoad_jsp.java:39)     org.apache.jasper.runtime.HttpJspBase.destroy(HttpJspBase.java:61)     org.apache.jasper.servlet.JspServletWrapper.destroy(JspServletWrapper.java:428)     org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:139)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)     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)

2 个答案:

答案 0 :(得分:4)

如果dataBean是一个Page对象,那么我认为如下修改你的JSTL会使它编译。我不认为你的第二个forEach正在调用一个返回Collection对象迭代的方法。我相信可以删除第三个嵌套的forEach,因为第二个嵌套的forEach将使用MyBean对象。

   <c:forEach items="${dataBean.columnList}" var="column" varStatus="rowStatus">
            <c:forEach items="${column.columnList}" var="mybean" varStatus="rowCount">
                <div>hello 1..2..3..<c:out value="${mybean.description}"/></div>
            </c:forEach>
    </c:forEach>

答案 1 :(得分:0)

尝试替换

中的第二行
<c:forEach items="${column1}" var="column2" varStatus="rowCount">

<c:forEach items="${column1.columnList}" var="column2" varStatus="rowCount">