开发者

nested c:foreach in jstl

开发者 https://www.devze.com 2023-02-14 21:43 出处:网络
I am working on JSTL to retrieve values from one bean. It contains one bean inside and this bean contains another bean.

I am working on JSTL to retrieve values from one bean. It contains one bean inside and this bean contains another bean. sample beans look like this

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
    }

}

And I need to access MyBean values. How can i retrieve? i tried below way...But Jsp is not compiled.

<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>

full exception description 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.开发者_如何学Pythonservlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


If dataBean is a Page object then I think modifying your JSTL as follows will make it compile. I don't think your second forEach was calling a method that returned a Collection object to iterate over. I believe the third nested forEach can be removed because by the second one you will be working with MyBean objects.

   <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>


Try to replace second row from

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

to

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

精彩评论

暂无评论...
验证码 换一张
取 消