Posted on 2005-12-09 10:43
Terry的Blog 閱讀(1236)
評論(0) 編輯 收藏 所屬分類:
java語言 、
web開發 、
轉載
STRUTS的 ActionForm到現在為止,出現了最少三種方式: 普通的,動態的和懶的.
所以你在你自已的開發中,可以有很多選擇,如果你安全第一,可以用普通的.如果你更喜歡XML,則用動態的.
如果你很懶,那就用Lazy ActionForm.? available in Version 1.2.6 onwards
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 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調用
java代碼:?
|
public ActionForward execute(ActionMapping mapping, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ActionForm form, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?HttpServeletRequest request, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?HttpServletResponse response)throwsException{? ????// 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 }
|
在ACTION中,你可以使用 BeanUtils 1.7.0的特性,把dynaForm一次性拷貝到HIBERNATE的POJO中去!
轉載地址:http://forum.javaeye.com/viewtopic.php?t=17441