`
可爱の小猪
  • 浏览: 104862 次
  • 性别: Icon_minigender_1
  • 来自: 南充
社区版块
存档分类
最新评论

DispatchAction, LookupDispatchAction, MappingDispatchAction深入分析

阅读更多

首先我们来看一下它们三者之间的关系

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中参数为空,则执行Actionunspecified方法


LookupDispatchAction
public abstract class LookupDispatchAction extends DispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由 ActionMappingparameter属性决定。它适合在一个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
它的相应方法的执行由 ActionMappingparameter名决定,注意这里和LookupDispatchAction不同,LookupDispatchAction的相应方法的执行由 ActionMappingparameter属性决定,


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的详细说明


DispatchAction,LookupDispatchAction,MappingDispatchAction
1) DispatchAction
就是在struts-config中用parameter参数配置一个表单字段名,这个字段的值就是最终替代execute被调用的方法. 例如parameter="method"request.getParameter("method")="save",其中"save"就是MethodNamestruts的请求将根据parameter被分发到"save"或者"edit"或者什么。但是有一点,save()或者edit()等方法的声明和execute必须一模一样。

2) LookupDispatchAction
继承DispatchAction, 用于对同一个页面上的多个submit按钮进行不同的响应。其原理是,首先用MessageResource将按钮的文本和ResKey相关联,例如button.save=保存;然后再复写getKeyMethodMap(), ResKeyMethodName对应起来, 例如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是等价的, saveedit之间没有任何直接的关系, 而事实呢,它们是同一个业务模型的两种不同操作。 我觉得这就是一个问题,对于saveedit这两种请求, 我后台逻辑有可能只是调用service的方法那一句不一样,其他代码是完全一致的(例如错误处理, 日志记录等)。因此我想出了这个小东西,在execute方法内部进行局部分解。

上面有人说看iBATIS的代码看到过类似的。的确,iBATIS实现了一个BeanAction,将ActionFormAcion写到了一个类里,然后通过一个ThreadLocal的局部变量在各个方法之间传递actionMapping, request, response这些参数。其每个方法的声明变得非常简单,返回的也是字符串forward名。

我在很大程度上是受了Clinton Begin的一些启发,但是BeanAction实现的框架稍微有些大,改写了struts的根本模式。而且作者也说,该框架没有经过实际应用的测试,所以我不想用。

楼顶的帖子我是想说的尽量全面些,所以把service,dao什么的都写进去了。有人说为什么不用setDao, 我不太明白是什么意思。是不是用spring直接注入呢?那样的话,必须用spring的代理,感觉不是很稳定而且大大增加了配置工作。
有人说xxxDao是局部变量,线性不安全。我也是想问问这个,象这种DAOService类,其本身一旦建立,是不会进行任何修改的(但是因为要从spring取,所以不能声明为final)。是不是可以理解为提供一些静态方法(例如saveWorld)的工具类呢?

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics