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
}
你就可以通过这样的方法来访问你的程序:

如果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>
 
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值