惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 站得更高,看得更远

使用ServletContextListener在服务器启动和关闭时创建和关闭缓存 Tomcat的class加载的优先顺序一览 汉字转拼音缩写 - 站得更高,看得更远 - 博客园 tomcat 中解决打开xls文件的乱码问题 鼠标在图片上滚动放大或者缩小图片的代码 - 站得更高,看得更远 - 博客园 弹出窗口居中 - 站得更高,看得更远 弹出窗口大全(js) - 站得更高,看得更远 网页常用小技巧(JavaScript) - 站得更高,看得更远 - 博客园 Javascript 小技巧集 Strus 2的新表单标志的使用 Struts 2与AJAX(第二部分) Struts 2与AJAX(第一部分) Struts 2中的OGNL 常用的Struts 2.0的标志(Tag)介绍 在Struts 2.0中国际化(i18n)您的应用程序 在Struts 2.0中实现表单数据校验(Validation) 在Struts 2中实现IoC - 站得更高,看得更远 在Struts 2中实现文件上传 转换器(Converter)——Struts 2.0中的魔术师 - 站得更高,看得更远
LookupDispatchAction, MappingDispatchAction深入分析
站得更高,看得更远 · 2008-05-09 · via 博客园 - 站得更高,看得更远

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

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>