Samuel Learning

  • 联系
  •  
  • 管理

文章分类(203)

  • 01 ASP/HTML(6)
  • 02 Script/CSS(16)
  • 03 C/C++
  • 04 XML(4)
  • 05 微软编程(26)
  • 06 J2EE(60)
  • 09 Linux(14)
  • 10 Database(27)
  • 11 报表打印
  • 12 打包安装(1)
  • 13 模式重构(2)
  • 14 系统架构
  • 15 敏捷开发(11)
  • 16 案例分析(30)
  • 17 Workflow(1)
  • 18 配置管理(1)
  • 19 项目管理
  • 20 英语(4)

新闻分类(52)

  • CXF学习
  • Hibernate学习(1)
  • iBatis学习(16)
  • Struts1学习(1)
  • Struts2学习(34)

.NET资源

  • adxmenu
  • C# Open Source
  • DNN Directory
  • M2Land
  • Windows Form FAQ
  • 中国DNN
  • 中国DNN联盟

Ajax

  • DoJo
  • GWT
  • JQuery
  • jquery autocomplete
  • jquery flexgrid
  • JQuery Form
  • jquery masked input
  • JQuery UI
  • jquery validation
  • Jquery 图表
  • jquery报表
  • jquery插件集合
  • Qooxdoo
  • Tibco GI
  • YUI
  • YUI-EXT
  • ZeroKode

Java开源

  • ABLE
  • Agile Tao
  • Ajax4JSF
  • Alfresco
  • AppFuse
  • Compiere
  • Equinox
  • Findbugs
  • Geronimo
  • Grails
  • Harmony
  • Hibernate论坛
  • JAG
  • Java开源大全
  • Java视线论坛
  • jBPM工作流
  • JSFTutorials
  • Nereide ERP
  • Ofbiz ERP
  • Opentaps ERP
  • operamasks
  • Petstore
  • Prototype.js
  • RIFE
  • Runa
  • SpringSide
  • Struts Menu
  • Sun Glassfish
  • Trails
  • YUI4JSF
  • 满江红

Mobile

  • Sencha

WEB资源

  • DHTML中心
  • DHTML参考手册
  • DHTML文档
  • EclipsePlugin
  • Firebug
  • GRO Clinux
  • jMaki
  • JSTL文档
  • LoadIcon
  • Openlaszlo
  • Struts Menu 展示
  • Web Test Tools
  • WebCtrs
  • Webdeveloper
  • 中国RIA开发者论坛

Workflow

  • E-Workflow
  • JBPM
  • OpenWFE
  • OSWorkflow
  • WFMC
  • Workflow Research

其他连接

  • confach
  • CPP
  • ejay
  • Giovanni
  • 丹佛
  • 交大e-learning
  • 交大研究生院
  • 可恶的猫
  • 天天@blog
  • 我的相册
  • 阿飞

大牛人

  • 32篇JBPM
  • David.Turing
  • HongSoft@业务集成
  • Joel
  • Koen Aers
  • Martinfowler
  • Raible Matt
  • Raible Wiki
  • Scott W.Ambler
  • Tom Baeyens
  • Uncle Bob
  • 一个世界在等待
  • 子在川上曰
  • 小布老师
  • 小明
  • 差沙
  • 徐昊
  • 江南白衣
  • 汪博士
  • 汪小金
  • 银狐999

开源软件

  • 2Bizbox ERP
  • CompiereCRM&ERP
  • EGW
  • Vtiger CRM
  • webERP

敏捷

  • Canoo
  • Cruisecontrol
  • DBUnit
  • EL4Ant
  • Extreme Programming
  • Fit
  • Fitnesse
  • JFrog
  • Liquibase
  • Maven
  • MockObjects
  • Selenium
  • Squish
  • xpairtise
  • XPlanner
  • XProgramming
  • 敏捷联盟

数据库

  • Oracle 中国
  • Oracle-ERP
  • Oracle在线社区

未归类

  • Aquarius Orm Studio
  • mambo建站系统
  • Oracle产品下载
  • 远程同步管理工具Capivara

经典框架

  • Apache Shale
  • formdef-struts
  • FreeMarker 主页
  • JBoss Seam
  • JSF 中心
  • JSF 入门应用
  • JSF中国
  • MyFaces官方
  • Spring 社区
  • Spring专业网站
  • Spring中文论坛
  • Spring参考手册
  • Spring官方网站
  • strecks-struts
  • Struts1
  • Struts2
  • Struts-layout
  • StrutsWiKi
  • Tapestry WIKI
  • Tapestry 官方
  • Tapestry4开发指南
  • Tapestry中文文档
  • Webwork2文档
  • Wicket

网络教程

  • Laliluna
  • RoseIndia
  • Sang Shin
  • Visualbuilder

著名站点

  • Buildix
  • Dev2Dev
  • IBM dev中国
  • InfoQ
  • ITPub
  • Java Eye
  • Java Research
  • JavaRead
  • JavaWorldTW
  • Matrix
  • PHP100
  • PHPX
  • SpringSideWiKi
  • TheServerSide
  • TWPHP
  • 中国工作流论坛

项目管理

  • 管理人网

最新评论

View Post

Dispatch-Struts-Examples1.3.8

 

Dispatch-Struts-Examples1.3.8

l Dispatch Action

An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.

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

<action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction" name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/>

which will use the value of the request parameter named "method" to pick the appropriate "execute" method, which must have the same signature (other than method name) of the standard Action.execute method. For example, you might have the following three methods in the same action:

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

and call one of the methods with a URL like this:

http://localhost:8080/myapp/saveSubscription.do?method=update

NOTE - All of the other mapping characteristics of this action must be shared by the various handlers. This places some constraints over what types of handlers may reasonably be packaged into the same DispatchAction subclass.

NOTE - If the value of the request parameter is empty, a method named unspecified is called. The default action is to throw an exception. If the request was cancelled (a html:cancel button was pressed), the custom handler cancelled will be used instead. You can also override the getMethodName method to override the action's default handler selection.

以上是JavaDoc提供的描述,除了http://localhost:8080/myapp/saveSubscription.do?method=update

这种请求外,也可以使用定义一个名叫method的隐藏域。

Example就是定义了一个dispatchMethod的隐藏域.

如果jsp里的隐藏域定义的方法在action里没有定义会NoSuchMethodException

如果jsp里的隐藏域定义的方法是execute,会有异常ServletException

如果jsp里请求的action对应的mappingaction配置没有指定parameter,会有ServletException

l MappingDispachAction

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

如果mappingaction配置的方法在action里没有定义会NoSuchMethodException

如果mappingaction配置的方法是execute,会有异常ServletException

如果jsp里请求的action对应的mappingaction配置没有指定parameter,会有ServletException

l LookupDispachAction

An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. To configure the use of this action in your struts-config.xml file, create an entry like this:

   <action path="/test"

           type="org.example.MyAction"

           name="MyForm"

          scope="request"

          input="/test.jsp"

      parameter="method"/>

which will use the value of the request parameter named "method" to locate the corresponding key in ApplicationResources. For example, you might have the following ApplicationResources.properties:

    button.add=Add Record

    button.delete=Delete Record

And your JSP would have the following format for submit buttons:

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

Your subclass must implement both getKeyMethodMap and the methods defined in the map. An example of such implementations are:

 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");

 }

Notes - If duplicate values exist for the keys returned by getKeys, only the first one found will be returned. If no corresponding key is found then an exception will be thrown. You can override the method unspecifiedto provide a custom handler. If the submit was cancelled (a html:cancel button was pressed), the custom handler cancelled will be used instead.

l ActionDispatcher

Action helper class that dispatches to a public method in an Action.

This class is provided as an alternative mechanism to using DispatchAction and its various flavours and means Dispatch behaviour can be easily implemented into any Action without having to inherit from a particular super Action.

To implement dispatch behaviour in an Action class, create your custom Action as follows, along with the methods you require (and optionally "cancelled" and "unspecified" methods):

   public class MyCustomAction extends Action {

       protected ActionDispatcher dispatcher

                = new ActionDispatcher(this, ActionDispatcher.MAPPING_FLAVOR);

       public ActionForward execute(ActionMapping mapping,

                                    ActionForm form,

                                    HttpServletRequest request,

                                    HttpServletResponse response)

                           throws Exception {

           return dispatcher.execute(mapping, form, request, response);

       }

   }

It provides three flavours of determing the name of the method:

DEFAULT_FLAVOR - uses the parameter specified in the struts-config.xml to get the method name from the Request (equivalent to DispatchAction except uses "method" as a default if the parameter is not specified in the struts-config.xml).

DISPATCH_FLAVOR - uses the parameter specified in the struts-config.xml to get the method name from the Request (equivalent to DispatchAction).

MAPPING_FLAVOR - uses the parameter specified in the struts-config.xml as the method name (equivalent to MappingDispatchAction).

l EventDispatchAction

Action that dispatches to to one of the public methods that are named in the parameter attribute of the corresponding ActionMapping and matches a submission parameter. This is useful for developers who prefer to use many submit buttons, images, or submit links on a single form and whose related actions exist in a single Action class.

The method(s) must have the same signature (other than method name) of the standard Action.execute method.

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="save,back,recalc=recalculate,default=save"/>

 save,back,recalc和submit的Property关联

where parameter contains three possible methods and one default method if nothing matches (such as the user pressing the enter key).

For utility purposes, you can use the key=value notation to alias methods so that they are exposed as different form element names, in the event of a naming conflict or otherwise. In this example, the recalc button (via a request parameter) will invoke the recalculate method. The security-minded person may find this feature valuable to obfuscate and not expose the methods.

The default key is purely optional. If this is not specified and no parameters match the list of method keys, null is returned which means the unspecified method will be invoked.

The order of the parameters are guaranteed to be iterated in the order specified. If multiple buttons were accidently submitted, the first match in the list will be dispatched.

l EventActionDispatcher

An Action helper class that dispatches to to one of the public methods that are named in the parameter attribute of the corresponding ActionMapping and matches a submission parameter. This is useful for developers who prefer to use many submit buttons, images, or submit links on a single form and whose related actions exist in a single Action class.

The method(s) in the associated Action must have the same signature (other than method name) of the standard Action.execute method.

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="save,back,recalc=recalculate,default=save"/>

 

where parameter contains three possible methods and one default method if nothing matches (such as the user pressing the enter key).

For utility purposes, you can use the key=value notation to alias methods so that they are exposed as different form element names, in the event of a naming conflict or otherwise. In this example, the recalc button (via a request parameter) will invoke the recalculate method. The security-minded person may find this feature valuable to obfuscate and not expose the methods.

The default key is purely optional. If this is not specified and no parameters match the list of method keys, null is returned which means the unspecified method will be invoked.

The order of the parameters are guaranteed to be iterated in the order specified. If multiple buttons were accidently submitted, the first match in the list will be dispatched.

To implement this dispatch behaviour in an Action, class create your custom Action as follows, along with the methods you require (and optionally "cancelled" and "unspecified" methods):

   public class MyCustomAction extends Action {

       protected ActionDispatcher dispatcher = new EventActionDispatcher(this);

       public ActionForward execute(ActionMapping mapping,

                                    ActionForm form,

                                    HttpServletRequest request,

                                    HttpServletResponse response)

                           throws Exception {

           return dispatcher.execute(mapping, form, request, response);

       }

   }

与ActionDispatcher类似,是一个helper类,为了使普通的action达到与EventDispatchAction一样的功能

posted on 2008-07-17 10:12 MingIsMe 阅读(167) 评论(0)  编辑  收藏 所属分类: 16 案例分析

 
Powered by:
BlogJava
Copyright © MingIsMe