它的出现在只要为了解决两个问题
1. 如果嵌套层次结构中的一个对象发生改变,则必须更新每个嵌套的引用。
2. 当使用一个具有若干字段的嵌套对象时需要重复列出每个字段的全部嵌套层次结构。如“student.family.fatherName”。
大部分嵌套标记跟html一样,有三个新的标记
nested:nest
定义一个嵌套级别
nested:writeNesting
输出当前字段的完整嵌套级别
nested:root
定义根级别
注意:当定义一个嵌套的JavaBean时,初始化时要给他创建实例。如:
SomeBean some = new SomeBean();而不是想下面一样简单的声明:
SomeBean some;
一个实例
/taglib/nest/index.jsp:
<html:form action="/student">
<nested:nest property="student">
姓名:<nested:text property="name"/><br>
年龄:<nested:text property="age"/><br>
<nested:nest property="family">
父亲姓名:<nested:text property="fatherName"/><br>
母亲姓名:<nested:text property="motherName"/><br>
</nested:nest>
<nested:nest property="contact">
联系电话:<nested:text property="phoneNumber"/><br>
电子邮箱:<nested:text property="email"></nested:text><br>
<nested:nest property="address">
所在省:<nested:text property="province"/><br>
所在市:<nested:text property="city"/><br>
详细地址:<nested:text property="detail"/><br>
邮编:<nested:text property="postCode"/><br>
</nested:nest>
</nested:nest>
</nested:nest>
<nested:hidden property="time" value="<%=cc%>"></nested:hidden>
<html:submit>提交</html:submit><html:reset>清除</html:reset><br>
</html:form>
/WEB-INF/struts-config.xml:
<form-bean name="studentForm" type="cn.rolia.struts.nest.StudentForm" />
<action
attribute="studentForm"
input="/taglib/nest/index.jsp"
name="studentForm"
path="/student"
scope="request"
type="cn.rolia.struts.nest.StudentAction">
<forward name="success" path="/taglib/nest/show.jsp" />
<forward name="failure" path="/taglib/nest/index.jsp" />
cn.rolia.struts.nest.StudentForm.java:
public class StudentForm extends ActionForm {
private String time;
private StudentBean student = new StudentBean();
. . .
}
cn.rolia.struts.nest.StudentAction.java:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudentForm studentForm = (StudentForm) form;// TODO Auto-generated method stub
return mapping.findForward("success");
}
package cn.rolia.struts.bean.student.StudentBean.java:
private String name;
private String age;
private ContactBean contact = new ContactBean();
private FamilyBean family = new FamilyBean();
cn.rolia.struts.bean.student.ContactBean.java:
String phoneNumber;
String Email;
AddressBean address = new AddressBean();
cn.rolia.struts.bean.student.FamilyBean.java:
String fatherName;
String motherName;
cn.rolia.struts.bean.student.AddressBean.java:
String country;
String province;
String city;
String postCode;
String detail;
/taglib/nest/show.jsp:
<body>
学生信息<br>
<nested:root name="studentForm">
时间:<nested:write property="time"/><br>
<nested:nest property="student">
姓名:<nested:write property="name"/><br>
<nested:nest property="family">
父亲:<nested:write property="fatherName"/><br>
</nested:nest>
<nested:nest property="contact">
联系邮箱:<nested:write property="email"/><br>
</nested:nest>
</nested:nest>
</nested:root>
</body>
评论