- 论坛徽章:
- 0
|
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>
JSP网页
代码
html:form action="/myActionPath">
h2>Simple Property Exampleh2>
Customer Number: html:text property="custNo"/>
Customer Name: html:text property="custName"/>
h2>Mapped Property Exampleh2>
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 Exampleh2>
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
}
在ACTION中,你可以使用 BeanUtils 1.7.0的特性,把dynaForm一次性拷贝到HIBERNATE的POJO中去!
public class dynaForm extends LazyValidatorForm{
private Address address= new Address();
private Map addressMap = new Map();
//...some get/set
}
h2>Mapped Property Exampleh2>
Street: html:text property="addressMap(address.street)"/>
Town: html:text property="addressMap(address.town)"/>
State: html:text property="addressMap(address.state)"/>
Country: html:text property="addressMap(address.country)"/>
//此时addressMap中存放每一行address对象
Map addressMap = (Map)dynaForm.get("address");
代码
logic:iterate id="products" property="products">
Product Code: html:text name="products" property="addressMap(address.street)" indexed="true"/>
Product Description: html:text name="products" property="addressMap(address.town)" indexed="true"/>
Product Price: html:text name="products" property="addressMap(address.state)" indexed="true"/>
logic:iterate>
然后
代码
List products = (List)dynaForm.get("products"); // indexed
ap address = (Map) products.get(0);
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/38180/showart_421589.html |
|