Struts 1之DispatchAction

时间:2021-10-19 04:24:01

DispatchAction是struts 1 的内置通用分发器

import org.apache.struts.actions.DispatchAction;

public class UserAction extends DispatchAction {

      public ActionForward execute(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request,HttpServletResponse response)
                   throwsException {
            //调用业务逻辑操作

                    //一定要执行这句话,或者干脆把execute()方法去掉
           return super.execute();
     }

     public ActionForward list(ActionMapping mapping, ActionForm form,
                   HttpServletRequest request,HttpServletResponse response)
                   throwsException {
            //调用业务逻辑操作

            return mapping.findForward("list_success");
     }

     public ActionForward del(ActionMappingmapping, ActionForm form,
                   HttpServletRequest request,HttpServletResponse response)
                   throws Exception {
           //调用业务逻辑操作

           return mapping.findForward("del_success");
     }

     public ActionForward add(ActionMappingmapping, ActionForm form,
                   HttpServletRequest request,HttpServletResponse response)
                   throwsException {

            //调用业务逻辑操作

            return mapping.findForward("add_success");    }

}

上面这个Action有三个跟业务相关的方法list()、add()、del();Struts将请求转入这个Action之后,会根据一个参数名称来决定要执行哪个方法,这个参数的名字和Action内的方法名字一致,这个参数需要在<action-mapping>中配置,一般为action或method

 <action
             name="firstForm"
             path="/user"
             parameter="method"            <!-- 分发器参数-->
             type="com.clf.struts.action.HelloAction"     />

DynaActionForm

DynaActionForm不需要写action,它的使用是配置式的,比普通的FormBean多了<form-property>

<form-beans>
   <form-bean name="dynaActionForm" type="org.apache.struts.action.DynaActionForm">
   <span style="white-space:pre">	</span><form-property name="age"type="java.lang.Integer"/>
   <span style="white-space:pre">	</span><form-property name="name"type="java.lang.String"/>
   </form-bean>
</form-beans> 

在action中使用

DynaActionForm dynaForm = (DynaActionForm) form;
Sting name = (String) dynaForm.get("name");
Integer age = (Integer) dynaForm.get("age");

 

在JSP中跟使用常规的FormBean一样