DispatchAction和MappingDispatchAction区别
DispatchAction和MappingDispatchAction的区别
DispatchAction是MappingDispatchAction的父类,
用同一表单提交到同一个act
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){}
}
在配置文件中写法如下,只需要写一个act
<act
<forward......./>
</act
注意parameter="method" 该配置指定要传来的表单需要一个名为method的属性来指定调用org.whatisjava.LoginAction类中的何种方法
表单代码如下:
<html:form act
<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>
<!--注意这里隐藏域 为了给act
<!--两个提交按钮不同的提交method.value的值分别为modify和add,是act
<input type="hiden" name="method" value="add"/>
<input type="submit" value='<bean:message key="button.add"/>' on
<input type="submit" value='<bean:message key="button.modify"/>' on
</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){}
}
配置文件为两个方法配置不同的<act
<!--这里的parameter指定的为act
<!--这里path为两个不同的path,而DispatchAction只需要一个path-->
<act
<forward......./>
</act
<act
<forward......./>
</act
表单的代码也有所不同
第一个submit按钮的on
第二个submit按钮的on
DispatchAction需要表单传不同的参数来区别不同的业务方法
MappingDispatchAction需要两个不同的act
自己写一个DispatchAction模仿struts提供的,用到反射技术
public class DispatchAction extends Act
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;
}
}