如何在 JSP 中使用嵌套的 foreach 标签
How to use nested foreach tag in JSP
我使用 Struts2 框架和 JSP。我想在 JSP 中嵌套 foreach
标签,但我在内部 foreach
标签处遇到错误。
迭代嵌套对象时出现错误。
<c:forEach var="emp" items="${dept.emplyees}">
异常:
Caused by: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274) ~[jstl-1.2.jar:1.2]
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238) ~[jstl-1.2.jar:1.2]
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155) ~[jstl-1.2.jar:1.2]
at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:291) ~[javax.servlet.jsp.jstl-api-1.2.1.jar:1.2.1]
at org.apache.jsp.views.Home.home_jsp._jspx_meth_c_005fforEach_005f1(home_jsp.java:364) ~[na:na]
at org.apache.jsp.views.Home.home_jsp._jspService(home_jsp.java:159) ~[na:na]
下面是我使用 POJO 和 Struts Action
字段的示例代码。
JSP代码:
<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>
操作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;
}
要遍历对象的 属性 它不应该是 null
并且有一个 getter 方法。
private List<Employee> emplyees = new ArrayList<>();
public List<Employee> getEmplyees() { return emplyees; }
在页面上显示此 属性 之前,最好有一些值。您可以在操作中执行此操作,或者在 prepare()
中更好,让您的操作实现 Preparable
interface.
Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the user's form input. If validation fails the Struts 2 framework returns the value "input"
but the "input"
action is not re-executed. Rather the view associated with the "input"
result is rendered to the user. Usually this view is the page that displayed the original form.
This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic look-up that that is accomplished in the Action
class's input method. Since the Action
class's input method is not re-executed when validation fails, the view page may no longer have access to the correct information to create the form or other display information.
我使用 Struts2 框架和 JSP。我想在 JSP 中嵌套 foreach
标签,但我在内部 foreach
标签处遇到错误。
迭代嵌套对象时出现错误。
<c:forEach var="emp" items="${dept.emplyees}">
异常:
Caused by: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274) ~[jstl-1.2.jar:1.2]
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238) ~[jstl-1.2.jar:1.2]
at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155) ~[jstl-1.2.jar:1.2]
at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:291) ~[javax.servlet.jsp.jstl-api-1.2.1.jar:1.2.1]
at org.apache.jsp.views.Home.home_jsp._jspx_meth_c_005fforEach_005f1(home_jsp.java:364) ~[na:na]
at org.apache.jsp.views.Home.home_jsp._jspService(home_jsp.java:159) ~[na:na]
下面是我使用 POJO 和 Struts Action
字段的示例代码。
JSP代码:
<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>
操作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;
}
要遍历对象的 属性 它不应该是 null
并且有一个 getter 方法。
private List<Employee> emplyees = new ArrayList<>();
public List<Employee> getEmplyees() { return emplyees; }
在页面上显示此 属性 之前,最好有一些值。您可以在操作中执行此操作,或者在 prepare()
中更好,让您的操作实现 Preparable
interface.
Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the user's form input. If validation fails the Struts 2 framework returns the value
"input"
but the"input"
action is not re-executed. Rather the view associated with the"input"
result is rendered to the user. Usually this view is the page that displayed the original form.This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic look-up that that is accomplished in the
Action
class's input method. Since theAction
class's input method is not re-executed when validation fails, the view page may no longer have access to the correct information to create the form or other display information.