`

struts nested标签

    博客分类:
  • java
 
阅读更多

nested 标签就是把表单直接对应到一个业务类对象,在FORM类中定义这个业务类对象就可,这个标签用起来也不难,而且还挺好用,比如说有个 表单类PersonForm,业务类Person,在PersonForm中定义 Person person=new Person(); 注意这里用new分配空间,不能只是这样Person person;

在Person类中定义 private String name; private String sex;

那么在.jsp中表单部分如下:

  1. //其他代码
  2. <html:formaction="/PersonAction">
  3. <nested:nestproperty="person">
  4. <nested:messagekey="use_nested.jsp.form.name"/>
  5. <nested:textproperty="name"size="18"/><br/>
  6. <nested:messagekey="use_nested.jsp.form.sex"/>
  7. <nested:textproperty="sex"size="18"/><br/>
  8. nested:nest>
  9. <html:submit>
  10. <bean:messagekey="use_nested.jsp.form.submit"/>
  11. html:submit>
  12. html:form>
  13. //其他代码

其中<nest> 理解就是定义一个类层次,它也可以嵌套使用,当业务类中还包含其他业务类时就可以使用它的嵌套,当使用嵌套时如果</nest>想显式知道嵌套的层次可以利用nested 标签的 nested:writeNesting 标签进行输出。比如现在我们再加一个业务类 Address ,类中数据元素为:private String country; private String province;
然后在 业务类 Person 中加上 private Address add=new Address();

结合 nested:writeNesting 后的.jsp 为:

xml 代码
  1. ///其他代码
  2. <html:formaction="/PersonAction">
  3. <nested:nestproperty="person">
  4. <nested:messagekey="use_nested.jsp.form.currentNesting"/>
  5. <nested:writeNesting/><br/>
  6. <nested:messagekey="use_nested.jsp.form.name"/>
  7. <nested:textproperty="name"size="18"/><br/>
  8. <nested:messagekey="use_nested.jsp.form.sex"/>
  9. <nested:textproperty="sex"size="18"/><br/>
  10. <nested:nestproperty="add">
  11. <nested:messagekey="use_nested.jsp.form.currentNesting"/>
  12. <nested:writeNesting/>
  13. <nested:messagekey="use_nested.jsp.form.country"/>
  14. <nested:textproperty="country"size="18"/><br/>
  15. <nested:messagekey="use_nested.jsp.form.province"/>
  16. <nested:textproperty="province"size="18"/><br/>
  17. nested:nest>
  18. nested:nest>
  19. <html:submit>
  20. <bean:messagekey="use_nested.jsp.form.submit"/>
  21. html:submit>
  22. html:form>
  23. ///其他代码

其中第二个 nested:writeNesting 会输出 person.add 表明当前类层次, 当你输出表单数据时 还可以定义一个顶层级别的 javaBean,这时候就利用 nested 的 nested:root 标签,比如要输出上个表单元素时,顶层级别就是 PersonForm ,其他则与上面是一样,如下:

xml 代码
  1. //其他代码
  2. <jsp:useBeanid="PersonForm"type="nestedtag.PersonForm"scope="request"/>
  3. <nested:rootname="PersonForm">
  4. <nested:nestproperty="person">
  5. <nested:messagekey="use_nested.jsp.form.name"/>
  6. <nested:writeproperty="name"/><br/>
  7. <nested:messagekey="use_nested.jsp.form.sex"/>
  8. <nested:writeproperty="sex"/><br/>
  9. <nested:nestproperty="add">
  10. <nested:messagekey="use_nested.jsp.form.country"/>
  11. <nested:writeproperty="country"/><br/>
  12. <nested:messagekey="use_nested.jsp.form.province"/>
  13. <nested:writeproperty="province"/><br/>
  14. nested:nest>
  15. nested:nest>
  16. nested:root>
  17. //其他代码

nested 的其他子标签则与 html子标签和 bean子标签的功能类似,就不用介绍了。

最后说明的是,利用nest 实现的功能也可以不用 nest 实现,只不过写 property 是把完整的属性路径写全就行了,因为比较简单看了下面的代码就明白了

xml 代码
  1. //其他代码
  2. <html:formaction="/PersonAction">
  3. <bean:messagekey="use_nested.jsp.form.name"/>
  4. <html:textproperty="person.name"size="18"/><br/>
  5. <bean:messagekey="use_nested.jsp.form.sex"/>
  6. <html:textproperty="person.sex"size="18"/><br/>
  7. <bean:messagekey="use_nested.jsp.form.country"/>
  8. <html:textproperty="person.add.country"size="18"/><br/>
  9. <bean:messagekey="use_nested.jsp.form.province"/>
  10. <html:textproperty="person.add.province"size="18"/><br/>
  11. <html:submit>
  12. <bean:messagekey="use_nested.jsp.form.submit"/>
  13. html:submit>
  14. html:form>
  15. //其他代码
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics