转摘---struts中的MappingDispatchAction类

来源:互联网 发布:华硕驱动软件 编辑:程序博客网 时间:2024/06/10 20:53
                 以下内容完全是个人做项目的经验,如果有不对或者遗漏的地方,希望大家共同探讨。在看struts文档的时候发现了org.apache.struts.action.MappingDispathAction这个类,以前没有见到过去找孙姐姐的struts的书发现没有提到这个类,估计是写书的时候struts还没有这个类,struts官方提供的资料在http://struts.apache.org/struts-doc-1.2.x/api/org/apache/struts/actions/MappingDispatchAction.html
以下是文档内容:

public class MappingDispatchAction
extends DispatchAction

An abstract Action that dispatches to a public method that is named by the parameter attribute of the corresponding ActionMapping. This is useful for developers who prefer to combine many related actions into a single Action class.

To configure the use of this action in your struts-config.xml file, create an entry like this:

   <action path="/saveSubscription"           type="org.example.SubscriptionAction"           name="subscriptionForm"          scope="request"          input="/subscription.jsp"      parameter="method"/> 

where 'method' is the name of a method in your subclass of MappingDispatchAction that has the same signature (other than method name) of the standard Action.execute method. For example, you might combine the methods for managing a subscription into a single MappingDispatchAction class using the following methods:

  • 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> 

NOTE - Unlike DispatchAction, mapping characteristics may differ between the various handlers, so you can combine actions in the same class that, for example, differ in their use of forms or validation. Also, a request parameter, which would be visible to the application user, is not required to enable selection of the handler method.

Since:
Struts 1.2
Version:
$Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $

下面我解释一下:  <action path="/createSubscription" <!--注意这里的path-->          type="org.example.SubscriptionAction"<!--这里是我们自己定一个那个类,这个类有一个方法是create,如果要调用create方法则在下面的parameter字段中指名同时注意这4个action配置的定义,这里的type都是SubscriptionAction他和其他的action比如DispatchAction最大的区别都在这里,实际上这里定义了多个action,但是他们的type都一致,无非是使用parameter字段来区分到底调用那个方法而已-->          parameter="create">      <forward name="success" path="/editSubscription.jsp"/>  </action>在jsp中,form的调用方法如下:<html:form action="/createSubscription.do"></html:form><html:form action="/editSubscription.do"></html:form><html:form action="/saveSubscription.do"></html:form><html:form action="/deleteSubscription.do"></html:form><html:form action="/listSubscriptions.do"></html:form>这样就回去执行SubscriptionAction的create方法那么这种配置和DispatchAction或者LookupDispatchAction有什么区别呢?最大的区别在于type上,DispatchAction配置给每一个继承它的类有一个<action配置就够了,然后利用parameter方法来指定调用的不同下面我们来看看实现同样的方法DispatchAction的配置和使用:  <action path="/SubscriptionAction"           type="org.example.SubscriptionAction"          parameter="method">      <forward name="success" path="/subscriptionList.jsp"/>  </action>在jsp中调用方法如下:<html:form action="/SubscriptionAction.do?method=create"></html:form><html:form action="/SubscriptionAction.do?method=edit"></html:form><html:form action="/SubscriptionAction.do?method=save"></html:form><html:form action="/SubscriptionAction.do?method=delete"></html:form><html:form action="/SubscriptionAction.do?method=list"></html:form>总结:MappingDispatchAction为每个不同的处理方法都要在struts-config.xml配置对应的action而DispatchAction只需要配置一个然后利用给parameter字段赋值来区分。从我做项目的经验来说,使用MappingDispatchAction恐怕是最方便最直接了,因为它最容易调试。因为根据form提交的action的不同就可以区分不同的方法(例如增加,删除,修改)但是缺点就是会是配置文件的内容变多,而DispatchAction方法的配置看上去比较简洁,每种方法各有千秋,就看你怎么用了,具体使用说明还是看struts的官方文档。

原创粉丝点击