struts1.x动态ActionForm

jsp页面(dynaform.jsp)

<h1>测试动态actionform</h1><hr>
    <form action="dyna.do" method="post">
    	<input type="text" name="name" /><br/>
    	<input type="text" name="age" /><br/>
    	<input type="submit" value="提交">
    </form><br>
    ${dynaForm.map.name }<br/>
    ${dynaForm.map.age }


xml文件配置


<form-beans>
    <!-- 
		配置动态actionform,type为DynaActionForm所在的全路径,也可继承该类配置自己的类路径,
		但是失去了减少actionform类的目的,动态actionform的验证可以plugin动态验证框架 -->
			<form-bean name="dynaForm" type="org.apache.struts.action.DynaActionForm">
		
		<!-- 
		配置的属性名要和jsp页面的属性名一致,动态actionform类继承了ActionForm类,
		它收集表单数据采用了一个map,类中有getMap方法获取map,所以可以在页面用el表达式导航获取其中的属性值
		-->
			<form-property name="name" type="java.lang.String" />
			<form-property name="age" type="java.lang.Integer" />
		</form-bean>
</form-beans>


<action path="/dyna" type="com.cao.action.DynaFormAction" name="dynaForm" scope="request">
				<forward name="success" path="/dynaform.jsp" />
			</action>



Action配置(DynaFormAction)

public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		//转型为org.apache.struts.action.DynaActionForm
		DynaActionForm daf = (DynaActionForm) form;
		//可以通过get属性名获取属性值
		System.out.println(daf.get("name"));
		System.out.println(daf.get("age"));
		return mapping.findForward("success");
	}

你可能感兴趣的:(apache,xml,jsp,框架,struts)