(4)DynaActionForm

1.创建页面文件

   <body>
      <form action="regist.do" method="get">
           name:<input type="text" name="userName">
           age :<input type="text" name="age">
           address:<input type="text" name="address">
           <input type="submit" name="button1">
      </form>
  </body>

2.struts-config.xml文件中配置DynaActionForm

     <form-beans >
    <form-bean name="DynaRegistForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="userName" type="java.lang.String" />
      <form-property name="age" type="java.lang.Integer" />
      <form-property name="address" type="java.lang.String" />
    </form-bean>
   </form-beans>
  <action-mappings >
    <action name="DynaRegistForm" path="/regist" type="com.rhcj.RegistAction">
      <forward name="success" path="/success.jsp" />
    </action>
  </action-mappings>

3. 在Action中使用DynaActionForm

    public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  //页面对象
 
 DynaActionForm dynaForm = (DynaActionForm)form;
  String name = dynaForm.getString("userName");
  Integer age = (Integer)dynaForm.get("age");
  String address = dynaForm.getString("address");
  //封装业务对象BO
  Student s = new Student();
  s.setUserName(name);
  s.setAge(age);
  s.setAddress(address);
  StudentBiz sBiz = new StudentBiz();
  sBiz.save(s);
  return mapping.findForward("success");
 }

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