STRUTS的 ActionForm到现在为止,出现了最少三种方式: 普通的 ,动态的 和 懒惰的 .
所以你在你自已的开发中,可以有很多选择,如果你安全第一,可以用普通的.如果你更喜欢XML,则用动态的.
如果你很懒,那就用Lazy ActionForm.
STRUTS提供的这三种ActionForm方式,要实际应用中你只要选择一种就可以了.
下面说说Lazy ActionForm:
如果你喜欢STRUTS的强大的功能的特性(就比如这个ActionForm有多种选择),又喜欢快捷, Lazy ActionForm对你来说是一个好消息. 这个有点类似于WW2中值得称道的一个特性,可以减少编写ActionForm的麻烦.(STRUTS正在把WW2中好的东西都吸收进来了,难怪这两个东西以后会合并为STRUTS IT).
示例代码如下:
struts-config.xml配置
<struts-config>
<form-beans>
<form-beanname="lazyForm"type="org.apache.struts.validator.LazyValidatorForm"/>
</form-beans>
<action-mappings>
<actionpath="/myActionPath"type="myPackage.MyAction"name="lazyForm"validate="true"/>
</action-mappings>
</struts-config>
<form-beans>
<form-beanname="lazyForm"type="org.apache.struts.validator.LazyValidatorForm"/>
</form-beans>
<action-mappings>
<actionpath="/myActionPath"type="myPackage.MyAction"name="lazyForm"validate="true"/>
</action-mappings>
</struts-config>
JSP网页
<html:formaction="/myActionPath">
<h2>SimplePropertyExample</h2>
CustomerNumber:<html:textproperty="custNo"/>
CustomerName:<html:textproperty="custName"/>
<h2>MappedPropertyExample</h2>
Street:<html:textproperty="address(street)"/>
Town:<html:textproperty="address(town)"/>
State:<html:textproperty="address(state)"/>
Country:<html:textproperty="address(country)"/>
<h2>IndexedPropertyExample</h2>
<logic:iterateid="products"property="products">
ProductCode:<html:textname="products"property="code"indexed="true"/>
ProductDescription:<html:textname="products"property="description"indexed="true"/>
ProductPrice:<html:textname="products"property="price"indexed="true"/>
</logic:iterate>
</html:form>
<h2>SimplePropertyExample</h2>
CustomerNumber:<html:textproperty="custNo"/>
CustomerName:<html:textproperty="custName"/>
<h2>MappedPropertyExample</h2>
Street:<html:textproperty="address(street)"/>
Town:<html:textproperty="address(town)"/>
State:<html:textproperty="address(state)"/>
Country:<html:textproperty="address(country)"/>
<h2>IndexedPropertyExample</h2>
<logic:iterateid="products"property="products">
ProductCode:<html:textname="products"property="code"indexed="true"/>
ProductDescription:<html:textname="products"property="description"indexed="true"/>
ProductPrice:<html:textname="products"property="price"indexed="true"/>
</logic:iterate>
</html:form>
action调用
publicActionForwardexecute(ActionMappingmapping,
ActionFormform,
HttpServeletRequestrequest,
HttpServletResponseresponse)throwsException{
//CastformtoDynaBean
DynaBeandynaForm=(DynaBean)form;
//UsetheDynaBean
StringcustNo=(String)dynaForm.get("custNo");//simple
Mapaddress=(Map)dynaForm.get("address");//mapped
Listproducts=(List)dynaForm.get("products");//indexed
...etcetc
}
ActionFormform,
HttpServeletRequestrequest,
HttpServletResponseresponse)throwsException{
//CastformtoDynaBean
DynaBeandynaForm=(DynaBean)form;
//UsetheDynaBean
StringcustNo=(String)dynaForm.get("custNo");//simple
Mapaddress=(Map)dynaForm.get("address");//mapped
Listproducts=(List)dynaForm.get("products");//indexed
...etcetc
}
在ACTION中,你可以使用 BeanUtils 1.7.0的特性,把dynaForm一次性拷贝到HIBERNATE的POJO中去!