java - How to use nested foreach in jsp -


i using struts2 framework jsp. want have nested foreach in jsp getting below error @ inner foreach.

getting error while iterating nested objects from.

<c:foreach var="emp" items="${dept.emplyees}">  

exception:

caused by: javax.servlet.jsp.jsptagexception: don't know how iterate on supplied "items" in &lt;foreach&gt;     @ org.apache.taglibs.standard.tag.common.core.foreachsupport.toforeachiterator(foreachsupport.java:274) ~[jstl-1.2.jar:1.2]     @ org.apache.taglibs.standard.tag.common.core.foreachsupport.supportedtypeforeachiterator(foreachsupport.java:238) ~[jstl-1.2.jar:1.2]     @ org.apache.taglibs.standard.tag.common.core.foreachsupport.prepare(foreachsupport.java:155) ~[jstl-1.2.jar:1.2]     @ javax.servlet.jsp.jstl.core.looptagsupport.dostarttag(looptagsupport.java:291) ~[javax.servlet.jsp.jstl-api-1.2.1.jar:1.2.1]     @ org.apache.jsp.views.home.home_jsp._jspx_meth_c_005fforeach_005f1(home_jsp.java:364) ~[na:na]     @ org.apache.jsp.views.home.home_jsp._jspservice(home_jsp.java:159) ~[na:na] 

below sample code pojo , struts action feilds.

jsp code:

<c:foreach var="dept" items="${deptlist}">  <c:out value="${dept.deptname}"/>  <c:foreach var="emp" items="${dept.emplyees}">  <c:out value="${emp.name}"/>  </c:foreach> </c:foreach> 

action class: testaction

class testaction{ list<department> deptlist  public list<department> getdeptlist() {         return deptlist;     }      public void setdeptlist(list<department> deptlist) {         this.deptlist = deptlist;     } } 

deprtment pojo

class department{  private string deptname  list<employee> emplyees;      public list<employee> getdeptlist() {         return emplyees;     }      public void setdeptlist(list<employee> emplyees) {         this.emplyees = emplyees;     } } 

employee pojo

class employee{     private string name; } 

to iterate on property of object should not null , have getter method.

private list<employee> emplyees = new arraylist<>(); public list<employee> getemplyees() { return emplyees; }   

before displaying property on page, nice have values. can in action, or better in prepare() , let action implement preparable interface.

often data used populate form control dynamically generated, perhaps database. when user submits form, struts 2 validation interceptor attempts validate user's form input. if validation fails struts 2 framework returns value "input" "input" action not re-executed. rather view associated "input" result rendered user. view page displayed original form.

this work-flow can cause problem if 1 or more of form fields or other data displayed depends on dynamic look-up that accomplished in action class's input method. since action class's input method not re-executed when validation fails, view page may no longer have access correct information create form or other display information.


Comments

Popular posts from this blog

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -