本文摘自javaweb整合开发,王者归来
虽然struts能够自动将提交的数据设置到FormBean属性中,但是FormBean并不是业务对象,还需要把数据从
FormBean中搬运到纯业务的业务组件POJO中(及domain实体中).因此Action中就要实现类似下面的代码:
使用nested标签就可以省略掉这些代码,nested标签能够使用struts把数据直接设置到业务组件POJO中,而不是FormBean上,如下例子:
在form bean内部定义一个Person类变量,注意person变量定义的时候要new一个对象。然后jsp页面利用nested标签把数据与对象绑定。代码如下:
注意引用nested标签库。nested标签与html标签作用,用法完全一致。事实上,nested标签的实现都继承自html标签,不同的是,nested标签的property属性可以使用"."操作符指定深层次的属性,例如person.birthday.提交表单后,数据也会直接赋值到这些深层次的属性上。但是某个属性为null,则引用该属性将会抛出异常。因此,本例中TileForm里new一个person对象。如果实际应用中遇到了异常,需要检查被引用的属性是否为空。
Action中的代码可以直接引用FormBean的业务对象。如下:
nested标签还有另一种写法:使用<nested:nest>标签。<nested:nest>能将属性按照层次编码,比前面的例子更加直观,例如:
<nested:nest>标签可以嵌套使用,代码中用了两层<nested:nest>标签。<nested:writeNesting>标签用于输出当前层次。代码中的两个<nested:writeNesting>分别输出Perosn,person.createDate.
<nested:root>用于声明最顶层的POJO,默认地,<nested:root>代表对象即为FormBean, 可以显示第申明nested标签的跟元素,
例如:
nested标签库还有<nested:defined>, <nested:equal>, <nested:message>等其它标签,这些标签与同名的html标签,logic标签用法一致。
虽然struts能够自动将提交的数据设置到FormBean属性中,但是FormBean并不是业务对象,还需要把数据从
FormBean中搬运到纯业务的业务组件POJO中(及domain实体中).因此Action中就要实现类似下面的代码:
PersonForm personForm = (PersonForm)form;
Person person = new Person();
person.setAccount(personForm.getAccount());
person.setName(personForm.getName());
person.setSecret(personForm.getSecret());
person.setCreateDate(personForm.getCreateDate());
使用nested标签就可以省略掉这些代码,nested标签能够使用struts把数据直接设置到业务组件POJO中,而不是FormBean上,如下例子:
public class TileForm extends ActionForm {
private String action; //action参数
private Person person = new Person(); //person对象
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
在form bean内部定义一个Person类变量,注意person变量定义的时候要new一个对象。然后jsp页面利用nested标签把数据与对象绑定。代码如下:
<html:form action="/title">
action: <html:text property="action"></html:text><br />
账号: <nested:text property="person.account" ></nested:text>
姓名:<nested:text property="person.name"></nested:text>
生日:<nested:text property="person.birthday" ></nested:text>
是否隐藏姓名:<nested:checkbox property="person.secret" >隐藏</nested:checkbox>
<html:submit"/>
</html:form>
注意引用nested标签库。nested标签与html标签作用,用法完全一致。事实上,nested标签的实现都继承自html标签,不同的是,nested标签的property属性可以使用"."操作符指定深层次的属性,例如person.birthday.提交表单后,数据也会直接赋值到这些深层次的属性上。但是某个属性为null,则引用该属性将会抛出异常。因此,本例中TileForm里new一个person对象。如果实际应用中遇到了异常,需要检查被引用的属性是否为空。
Action中的代码可以直接引用FormBean的业务对象。如下:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
TileForm tf = (TileForm)form;
if("add".equals(tf.getAction())){
Person person = tf.getPerson();
PersonDao pDao = new PersonDao();
pDao.addPerson(getDataSource(request).getConnection(), person);
return mapping.findForward("success");
}
}
nested标签还有另一种写法:使用<nested:nest>标签。<nested:nest>能将属性按照层次编码,比前面的例子更加直观,例如:
<html:form action="/title">
action: <html:text property="action"></html:text><br />
<nested:nest property="person">
当前层次:<nested:writeNesting /><br /> <!-- 输出当前层次 -->
账号: <nested:text property="account" ></nested:text>
姓名:<nested:text property="name"></nested:text>
生日:<nested:text property="birthday" ></nested:text>
是否隐藏姓名:<nested:checkbox property="secret" >隐藏</nested:checkbox>
</nested:nest>
<nested:nest property="createDate">
当前层次:<nested:writeNesting /><br /> <!-- 输出当前层次 -->
创建时间:<nested:text property="time"></nested:text>
</nested:nest>
<html:submit"/>
</html:form>
<nested:nest>标签可以嵌套使用,代码中用了两层<nested:nest>标签。<nested:writeNesting>标签用于输出当前层次。代码中的两个<nested:writeNesting>分别输出Perosn,person.createDate.
<nested:root>用于声明最顶层的POJO,默认地,<nested:root>代表对象即为FormBean, 可以显示第申明nested标签的跟元素,
例如:
<html:form action="/title">
<nested:root name="titleForm" >
<nested:text property="person.CreateDate.time" />
</nested:root>
</html:form>
nested标签库还有<nested:defined>, <nested:equal>, <nested:message>等其它标签,这些标签与同名的html标签,logic标签用法一致。