DispatchAction和MappingDispatchAction的区别
MappingDispatchAction为每个不同的处理方法都要在struts-config.xml配置对应的action而DispatchAction
只需要配置一个然后利用给parameter字段赋值来区分。从我做项目的经验来说,使用MappingDispatchAction恐怕是
最方便最直接了,因为它最容易调试。因为根据form提交的action的不同就可以区分不同的方法(例如增加,删除,修
改)但是缺点就是会是配置文件的内容变多,而DispatchAction方法的配置看上去比较简洁,每种方法各有千秋。
?
下面是一个例子:
?
DispatchAction是MappingDispatchAction的父类,
用同一表单提交到同一个action而需要两个不同方法业务做处理的解决方式说明两个Action的不同
DispatchAction:
一个类继承DispatchAction后,写自定义方法名(按照execute方法格式),分别处理不同的业务逻辑
public class LoginAction extends DispatchAction{
?public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){}
?
?public ActionForward modify(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){}
}
在配置文件中写法如下,只需要写一个action标签:
<action path="/login" type="org.whatisjava.LoginAction" name="loginForm" parameter="method">
<forward......./>
</action>
注意parameter="method" 该配置指定要传来的表单需要一个名为method的属性来指定调用org.whatisjava.LoginAction类中的何种方法
表单代码如下:
<html:form action="login.do">
<table>
<tr>
?<th><bean:message key="username"/></th> <!--bean:message读资源文件里指定的key-->
?<th><html:text property="username" size="15"></td>
</tr>
<tr>
?<th><bean:message key="pass"/></th> <!--bean:message读资源文件里指定的key-->
?<th><html:text property="pass" size="15"></td>
</tr>
<tr>
?<td>
<!--注意这里隐藏域 为了给action传一个parameter,配置文件中的parameter指定为这里的name为"method"-->
<!--两个提交按钮不同的提交method.value的值分别为modify和add,是action中的方法名-->
? <input type="hiden" name="method" value="add"/>
? <input type="submit" value='<bean:message key="button.add"/>' onClick="method.value='add'">
? <input type="submit" value='<bean:message key="button.modify"/>' onClick="method.value='modify'">
?</td>
</tr>
MappingDispatchAction:
一个类继承MappingDispatchAction
public class LoginAction extends MappingDispatchAction{
?public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){}
?
?public ActionForward modify(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){}
}
配置文件为两个方法配置不同的<action>,有几个方法就写几个action标签
<!--这里的parameter指定的为action类中的自定义方法的名字-->
<!--这里path为两个不同的path,而DispatchAction只需要一个path-->
<action path="/add" type="org.whatisjava.LoginAction" name="loginForm" parameter="add">
<forward......./>
</action>
<action path="/modify" type="org.whatisjava.LoginAction" name="loginForm" parameter="modify">
<forward......./>
</action>
表单的代码也有所不同
第一个submit按钮的onClick="document.loginForm.action='add.do'"
第二个submit按钮的onClick="document.loginForm.action='modify.do'"
DispatchAction需要表单传不同的参数来区别不同的业务方法
MappingDispatchAction需要两个不同的action标签指定不同的parameter来找对应方法,提交的path也不一样
自己写一个DispatchAction模仿struts提供的,用到反射技术
public class DispatchAction extends Action {
?private Map map = Collections.synchronizedMap(new HashMap());
?@Override
?public ActionForward execute(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response)
???throws Exception {
??String param = mapping.getParameter();
??String methodName = request.getParameter(param);
??Method method = (Method) map.get(methodName);
??if (method == null) {
???Class clazz = this.getClass();
???method = clazz.getMethod(methodName, new Class[] {
?????ActionMapping.class, ActionForm.class,
?????HttpServletRequest.class, HttpServletResponse.class });
???map.put(methodName, method);
??}
??ActionForward forward = (ActionForward) method.invoke(this,
????new Object[] { mapping, form, request, response });
??return forward;
?}
}
<script type="text/javascript"></script>