index.jsp
<li>测试dynaActionForm</li><br>
<form action="dynaactionform.do" method="post">
姓名:<input type="text" name="name"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" name="submit" value="保存"><br>
</form>
--------------------------------------------------------------------------
struts-config.xml
<form-beans>
<form-bean name="dynaActionForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="name" type="java.lang.String" />
<form-property name="age" type="java.lang.Integer" />
</form-bean>
</form-beans>
<action-mappings>
<action path="/dynaactionform"
type="com.bjsxt.struts.DynaActionFormTestAction"
name="dynaActionForm"
scope="request"
>
<forward name="success" path="/dynaactionform_success.jsp" />
</action>
</action-mappings>
------------------------------------------------
DynaActionFormTestAction
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
/**
* 测试动态ActionForm
* @author Administrator
*
*/
public class DynaActionFormTestAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm daf=(DynaActionForm)form;
String name=(String)daf.get("name");//说明,他会把,name,age放到map里做为key,而key的值是表单里收集的,这里的get()方法其实是调用Map中的值
Integer age=(Integer)daf.get("age");
//其实动态ActionForm只是把表单中的信息放入了Map中而已,用的时候在从Map中取出来
// System.out.println("name="+name);
//System.out.println("age="+age);
request.setAttribute("name", name);
request.setAttribute("age", age);
return mapping.findForward("success");
}
}
----------------------------------------------
dynaactionform_success.jsp
姓名:${dynaActionForm.map.name}<br><!--这个调用语句是在org.apache.struts.action.DynaActionForm;中的查找getMap()方法
年龄:${dynaActionForm.map.age }<br>