DispatchAction, LookupDispatchAction, MappingDispatchAction
1) DispatchAction就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。同时:删除以前的execute(----)方法,如:
-------
-------
public class UserAction extends DispatchAction
{
public ActionForward addUser (ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
// 增加用户业务的逻辑
return mapping.findForward(Constant. FORWARD_ADD );
}
public ActionForward delUser(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
// 删除用户业务的逻辑
return mapping.findForward(Constant. FORWARD_SUCCESS );
}
public ActionForward updateUser(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
// 更新用户业务的逻辑
return mapping.findForward(Constant. FORWARD_SUCCESS );
}
}
2) LookupDispatchAction继承DispatchAction, 用于对同一个页面上的多个submit按钮进行不同的响应。其原理是,首先用MessageResource将按钮的文本和ResKey相关联,例如button.save=save;然后再复写getKeyMethodMap(), 将ResKey和MethodName对应起来, 例如map.put("button.save", "save"); 其配置方法和DispatchAction是一样的, parameter属性为:method 如:
<html:submit property="method">
<bean:message key="button.save"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
注意:此Action一定要有对应的FormBean, 就是说一定要有name属性, 同时要删除此类的execute(-----)方法
资源文件如:
button.save=save
button.delete=delete
3) MappingDispatchAction是1.2新加的, 也继承自DispatchAction. 它实现的功能和上面两个区别较大, 是通过struts-config.xml将多个action-mapping映射到同一个Action类的不同方法上, 典型的配置就是:
<action-mappings>
<action path="/saveUser" type="logic.UserAction" parameter="save"></action>
<action path="/editUser" type="logic.UserAction" parameter="edit"></action>
</action-mappings>
然后UserAction继承MappingDispatchAction,其中有:
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
等方法