DynaActionForm(动态ActionForm)的使用
在struts中利用DynaActionForm(动态ActionForm)可以节省代码的编写。
1.在struts-config.xml中配置DynaActionForm:加入这个Form中有三个属性
<form-bean name="MyDynaActionForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="propertyOne" type="com.enrising.cpmis.web.dto.setup.propertyOne"/>
<form-property name="propertyTwo" type="java.lang.Object"/>
<form-property name="rebuildByDisaster" type="model.RebuildByDisaster"/>
</form-bean>
<action
path="/attchment"
type="web.struts.action.common.AttchmentAction"
name="MyDynaActionForm"
scope="request"
parameter="method"
unknown="false"
validate="true"
>
<forward name="initattachment" path="/business/common/annex.jsp" redirect="false"/>
</action>
2.jsp页面
<html:form action="/capitalSplitAction.do" method="post">
<html:hidden property="propertyOne.activityId" styleId="activityId" />
<bean:define id="dto" name="capitalSplitActionForm"
property="propertyOne" />
<table class="table" cellspacing="0" cellpadding="0">
<tr><td><html:hidden property="propertyOne.planName" /></td></tr>
<tr><td><html:hidden property="propertyTwo.planNameTwo" /></td></tr>
<tr><input name="submit1" type="button" class="bt_xiafa" value="下达" onclick="submitCapital();"></td></tr>
</table>
3.在Action中我们可以通过map的get方法来得到页面传到后台的Form属性对象(这个Action继承DispatchAction)
public ActionForward submitCapital(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response) throws Exception {
PropertyOne propertyOne = (PropertyOne)((DynaActionForm) form).get("propertyOne"); //得到页面传到后台的属性对象
PropertyTwo propertyTwo= (PropertyTwo)((DynaActionForm) form).get("propertyTwo");
//从而通过这个对象来得到它的属性
String planName = propertyOne.getPlanName();
……
//同理注册一个DynaActionForm
((DynaActionForm) form).getMap().put("PropertyOne", propertyOne);
}