`
llyzq
  • 浏览: 577677 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Action,DispatchAction,MappingDispatchAction,LookupDispatchAction的不同

阅读更多

http://blog.csdn.net/Jonthanoscar/archive/2007/08/09/1733779.aspx

 

首先我们来看一下它们三者之间的关系
java.lang.Object
|
+--org.apache.struts.action.Action
|
+--org.apache.struts.actions.DispatchAction
|
+--org.apache.struts.actions.LookupDispatchAction
|
+--org.apache.struts.actions.MappingDispatchAction

DispatchAction
定义
public abstract class DispatchAction extends Action
这是一个抽象的Action,它会根据request 中的parameter来执行相应的方法。通个这个Action类可以将不同的Action集中到一个Action文件中来。
Struts-config.xml:
<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>
在Action中要有相应的方法:
Public class demoAction extends DispatchAction{
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
}
你就可以通过这样的方法来访问你的程序:

http://localhost:8080/myapp/saveSubscription.do?method=update

如果parameter中参数为空,则执行Action中unspecified方法

LookupDispatchAction
public abstract class LookupDispatchAction extends DispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由 ActionMapping中parameter属性决定。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作。
struts-config.xml:
<action path="/test"
type="org.example.MyAction"
name="MyForm"
scope="request"
input="/test.jsp"
parameter="method"/>
ApplicationResources.properties:

button.add=Add Record
button.delete=Delete Record
JSP:

<html:form action="/test">
<html:submit property="method">
<bean:message key="button.add"/>
</html:submit>
<html:submit property="method">
<bean:message key="button.delete"/>
</html:submit>
</html:form>
在Action 中必须实现getKeyMethodMap:

protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("button.add", "add");
map.put("button.delete", "delete");
return map;
}

public ActionForward add(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do add
return mapping.findForward("success");
}

public ActionForward delete(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// do delete
return mapping.findForward("success");
}

MappingDispatchAction
public class MappingDispatchAction extends DispatchAction
它的相应方法的执行由 ActionMapping中parameter名决定,注意这里和LookupDispatchAction不同,LookupDispatchAction的相应方法的执行由 ActionMapping中parameter属性决定,

struts-config.xml:
 
<action path="/saveSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="method"/>
Action:

public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
for which you would create corresponding <action> configurations that reference this class:
 
<action path="/createSubscription"
type="org.example.SubscriptionAction"
parameter="create">
<forward name="success" path="/editSubscription.jsp"/>
</action>

<action path="/editSubscription"
type="org.example.SubscriptionAction"
parameter="edit">
<forward name="success" path="/editSubscription.jsp"/>
</action>

<action path="/saveSubscription"
type="org.example.SubscriptionAction"
parameter="save"
name="subscriptionForm"
validate="true"
input="/editSubscription.jsp"
scope="request">
<forward name="success" path="/savedSubscription.jsp"/>
</action>

<action path="/deleteSubscription"
type="org.example.SubscriptionAction"
name="subscriptionForm"
scope="request"
input="/subscription.jsp"
parameter="delete">
<forward name="success" path="/deletedSubscription.jsp"/>
</action>

<action path="/listSubscriptions"
type="org.example.SubscriptionAction"
parameter="list">
<forward name="success" path="/subscriptionList.jsp"/>
</action>

DispatchAction,LookupDispatchAction,MappingDispatchAction
1) DispatchAction就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。
2) LookupDispatchAction继承DispatchAction, 用于对同一个页面上的多个submit按钮进行不同的响应。其原理是,首先用MessageResource将按钮的文本和ResKey相关联,例如button.save=保存;然后再复写getKeyMethodMap(), 将ResKey和MethodName对应起来, 例如map.put("button.save", "save"); 其配置方法和DispatchAction是一样的, 使用时要这么写:
3) MappingDispatchAction是1.2新加的, 也继承自DispatchAction. 它实现的功能和上面两个区别较大, 是通过struts-config.xml将多个action-mapping映射到同一个Action类的不同方法上, 典型的配置就是:


然后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
等方法
可以看到, 不管怎么变化, 其实这些类都是把execute给分解开, 不管是save, edit还是其他什么方法, 其实都是和原来的execute是等价的, save和edit之间没有任何直接的关系, 而事实呢,它们是同一个业务模型的两种不同操作。 我觉得这就是一个问题,对于save和edit这两种请求, 我后台逻辑有可能只是调用service的方法那一句不一样,其他代码是完全一致的(例如错误处理, 日志记录等)。因此我想出了这个小东西,在execute方法内部进行局部分解。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Jonthanoscar/archive/2007/08/09/1733779.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics