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-bean name ="lazyForm" type ="org.apache.struts.validator.LazyValidatorForm" />
</ form-beans >
< action-mappings >
< action path ="/myActionPath" type ="myPackage.MyAction" name ="lazyForm" validate ="true" />
</ action-mappings >
</ struts-config >
< form-beans >
< form-bean name ="lazyForm" type ="org.apache.struts.validator.LazyValidatorForm" />
</ form-beans >
< action-mappings >
< action path ="/myActionPath" type ="myPackage.MyAction" name ="lazyForm" validate ="true" />
</ action-mappings >
</ struts-config >
JSP网页
<
html:form
action
="/myActionPath"
>
< h2 > Simple Property Example </ h2 >
Customer Number: < html:text property ="custNo" />
Customer Name: < html:text property ="custName" />
< h2 > Mapped Property Example </ h2 >
Street: < html:text property ="address(street)" />
Town: < html:text property ="address(town)" />
State: < html:text property ="address(state)" />
Country: < html:text property ="address(country)" />
< h2 > Indexed Property Example </ h2 >
< logic:iterate id ="products" property ="products" >
Product Code: < html:text name ="products" property ="code" indexed ="true" />
Product Description: < html:text name ="products" property ="description" indexed ="true" />
Product Price: < html:text name ="products" property ="price" indexed ="true" />
</ logic:iterate >
</ html:form >
< h2 > Simple Property Example </ h2 >
Customer Number: < html:text property ="custNo" />
Customer Name: < html:text property ="custName" />
< h2 > Mapped Property Example </ h2 >
Street: < html:text property ="address(street)" />
Town: < html:text property ="address(town)" />
State: < html:text property ="address(state)" />
Country: < html:text property ="address(country)" />
< h2 > Indexed Property Example </ h2 >
< logic:iterate id ="products" property ="products" >
Product Code: < html:text name ="products" property ="code" indexed ="true" />
Product Description: < html:text name ="products" property ="description" indexed ="true" />
Product Price: < html:text name ="products" property ="price" indexed ="true" />
</ logic:iterate >
</ html:form >
action调用
public
ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServeletRequest request,
HttpServletResponse response) throws Exception {
// Cast form to DynaBean
DynaBean dynaForm = (DynaBean)form;
// Use the DynaBean
String custNo = (String)dynaForm.get( " custNo " ); // simple
Map address = (Map)dynaForm.get( " address " ); // mapped
List products = (List)dynaForm.get( " products " ); // indexed
... etc etc
}
ActionForm form,
HttpServeletRequest request,
HttpServletResponse response) throws Exception {
// Cast form to DynaBean
DynaBean dynaForm = (DynaBean)form;
// Use the DynaBean
String custNo = (String)dynaForm.get( " custNo " ); // simple
Map address = (Map)dynaForm.get( " address " ); // mapped
List products = (List)dynaForm.get( " products " ); // indexed
... etc etc
}
在ACTION中,你可以使用 BeanUtils 1.7.0的特性,把dynaForm一次性拷贝到HIBERNATE的POJO中去!