免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1698 | 回复: 0
打印 上一主题 下一主题

几种DispatchAction的区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-02 21:18 |只看该作者 |倒序浏览

java.lang.Object
    org.apache.struts.action.Action
        org.apache.struts.actions.DispatchAction
            org.apache.struts.actions.LookupDispatchAction(Struts1.1)
            org.apache.struts.actions.EventDispatchAction(Struts1.2.9)
            org.apache.struts.actions.MappingDispatchAction(Struts1.2)
DispatchAction
public abstract class DispatchAction extends Action
这是一个抽象的Action,它会根据request 中的parameter来执行相应的方法。通个这个Action类可以将不同的Action集中到一个Action文件中来。
struts-config.xml:
action path="/subscription" type="org.example.SubscriptionAction" name="subscriptionForm"  scope="request" input="/subscription.jsp" parameter="method"/>
在Action中要有相应的方法:

public class SubscriptionAction 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/subscription.do?method=delete
http://localhost:8080/myapp/subscription.do?method=insert
http://localhost:8080/myapp/subscription.do?method=update
如果parameter中参数为空,则调用Action中的unspecified方法
LookupDispatchAction
public abstract class LookupDispatchAction extends DispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由ActionMapping中parameter属性决定。每个动作实际上就是标签的property属性值。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作。
struts-config.xml:

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

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

@ page pageEncoding="GBK"%>
@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
html>
  head>
    title>多提交演示title>
  head>
  body>
    html:form action="subscription">
      html:submit property="method">
        bean:message key="button.add"/>
      html:submit>
      html:submit property="method">
        bean:message key="button.delete"/>
      html:submit>
    html:form>
  body>
html>
在Action中必须实现getKeyMethodMap方法:

public class SubscriptionAction extends LookupDispatchAction {
    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 Exception {}
    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {}
}
EventDispatchAction
public class EventDispatchAction extends DispatchAction
通过这个Action抽象类继承DispatchAction,它的相应方法的执行由ActionMapping中parameter属性指定多个动作,中间用逗号(,)分隔。每个动作实际上就是标签的property属性值。它适合在一个form中有很多按钮,按不同的按钮则执行不同的操作。
struts-config.xml:
(parameter中的"recalc=recalculate"意思为标签的property属性值为recalc调用recalculate方法,出于安全考虑能够隐藏后台的业务方法名。"defaule=save"这是可选的,没有配置property属性的标签将调用defaule中设置的方法名,如果defaule没有指定任何参数,则调用Action中的unspecified方法)
action path="/subscription" type="org.example.SubscriptionAction" name="subscriptionForm" scope="request"
input="/subscription.jsp" parameter="save,back,recalc=recalculate,default=save"/>
JSP:

@ page pageEncoding="GBK"%>
@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
html>
  head>
    title>多提交演示title>
  head>
  body>           
    html:form action="subscription">
      html:submit property="save" value="保存"/>
      html:submit property="back" value="后退"/>
      html:submit property="recalc" value="重新计算"/>
    html:form>  
  body>
html>
在Action中要有相应的方法:

public class SubscriptionAction extends LookupDispatchAction {
    public ActionForward save(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {}
    public ActionForward back(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {}
    public ActionForward recalculate(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {}
}
MappingDispatchAction
public class MappingDispatchAction extends DispatchAction
它的相应方法的执行由ActionMapping中parameter名决定,注意这里和LookupDispatchAction不同,LookupDispatchAction的相应方法的执行由ActionMapping中parameter属性决定。
struts-config.xml:

action path="/createSubscription" type="org.example.SubscriptionAction" parameter="create">
      forward name="success" path="/createSubscription.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>
在Action中要有相应的方法:

public class SubscriptionAction extends MappingDispatchAction {
    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 {}
}
然后可以通过这样的方法来访问你的程序:

http://localhost:8080/myapp/create.do
http://localhost:8080/myapp/edit.do
http://localhost:8080/myapp/save.do
http://localhost:8080/myapp/delete.do
http://localhost:8080/myapp/list.do
如果parameter中参数为空,则调用Action中的unspecified方法


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21344/showart_2109763.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP