org.apache.struts.actions

Class ActionDispatcher

public class ActionDispatcher extends Object

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:

Since: Struts 1.2.7

Version: $Revision: 383720 $ $Date: 2006-03-07 00:07:59 +0000 (Tue, 07 Mar 2006) $

Field Summary
protected ActionactionInstance
The associated Action to dispatch to.
protected Classclazz
The Class instance of this DispatchAction class.
static intDEFAULT_FLAVOR
Indicates "default" dispatch flavor
static intDISPATCH_FLAVOR
Indicates flavor compatible with DispatchAction
protected intflavor
Indicates dispatch flavor
protected static Loglog
Commons Logging instance.
protected static MessageResourcesmessages
The message resources for this package.
protected HashMapmethods
The set of Method objects we have introspected for this class, keyed by method name.
static intMAPPING_FLAVOR
Indicates "mapping" dispatch flavor
protected Class[]types
The set of argument type classes for the reflected method call.
Constructor Summary
ActionDispatcher(Action actionInstance)
ActionDispatcher(Action actionInstance, int flavor)
Method Summary
protected ActionForwardcancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Dispatches to the target class' cancelled method, if present, otherwise returns null.

protected ActionForwarddispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name)
Dispatch to the specified method.
protected ActionForwarddispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name, Method method)
Dispatch to the specified method.
ActionForwardexecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it).
protected MethodgetMethod(String name)
Introspect the current class to identify a method of the specified name that accepts the same parameter types as the execute method does.
protected StringgetMethodName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String parameter)
Returns the method name, given a parameter's value.
protected StringgetParameter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Returns the parameter value as influenced by the selected {@link #flavor} specified for this ActionDispatcher.

protected booleanisCancelled(HttpServletRequest request)

Returns true if the current form's cancel button was pressed.

protected ActionForwardunspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Dispatches to the target class' unspecified method, if present, otherwise throws a ServletException.

Field Detail

actionInstance

protected Action actionInstance
The associated Action to dispatch to.

clazz

protected Class clazz
The Class instance of this DispatchAction class.

DEFAULT_FLAVOR

public static final int DEFAULT_FLAVOR
Indicates "default" dispatch flavor

DISPATCH_FLAVOR

public static final int DISPATCH_FLAVOR
Indicates flavor compatible with DispatchAction

flavor

protected int flavor
Indicates dispatch flavor

log

protected static Log log
Commons Logging instance.

messages

protected static MessageResources messages
The message resources for this package.

methods

protected HashMap methods
The set of Method objects we have introspected for this class, keyed by method name. This collection is populated as different methods are called, so that introspection needs to occur only once per method name.

MAPPING_FLAVOR

public static final int MAPPING_FLAVOR
Indicates "mapping" dispatch flavor

types

protected Class[] types
The set of argument type classes for the reflected method call. These are the same for all calls, so calculate them only once.

Constructor Detail

ActionDispatcher

public ActionDispatcher(Action actionInstance)

ActionDispatcher

public ActionDispatcher(Action actionInstance, int flavor)

Method Detail

cancelled

protected ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Dispatches to the target class' cancelled method, if present, otherwise returns null. Classes utilizing ActionDispatcher should provide a cancelled method if they wish to provide behavior different than returning null.

dispatchMethod

protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name)
Dispatch to the specified method.

dispatchMethod

protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name, Method method)
Dispatch to the specified method.

execute

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it). Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.

Parameters: mapping The ActionMapping used to select this instance form The optional ActionForm bean for this request (if any) request The HTTP request we are processing response The HTTP response we are creating

Throws: Exception if an exception occurs

getMethod

protected Method getMethod(String name)
Introspect the current class to identify a method of the specified name that accepts the same parameter types as the execute method does.

Parameters: name Name of the method to be introspected

Throws: NoSuchMethodException if no such method can be found

getMethodName

protected String getMethodName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String parameter)
Returns the method name, given a parameter's value.

Parameters: mapping The ActionMapping used to select this instance form The optional ActionForm bean for this request (if any) request The HTTP request we are processing response The HTTP response we are creating parameter The ActionMapping parameter's name

Returns: The method's name.

getParameter

protected String getParameter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Returns the parameter value as influenced by the selected {@link #flavor} specified for this ActionDispatcher.

Parameters: mapping The ActionMapping used to select this instance form The optional ActionForm bean for this request (if any) request The HTTP request we are processing response The HTTP response we are creating

Returns: The ActionMapping parameter's value

isCancelled

protected boolean isCancelled(HttpServletRequest request)

Returns true if the current form's cancel button was pressed. This method will check if the Globals.CANCEL_KEY request attribute has been set, which normally occurs if the cancel button generated by CancelTag was pressed by the user in the current request. If true, validation performed by an ActionForm's validate() method will have been skipped by the controller servlet.

Parameters: request The servlet request we are processing

See Also: CancelTag

unspecified

protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)

Dispatches to the target class' unspecified method, if present, otherwise throws a ServletException. Classes utilizing ActionDispatcher should provide an unspecified method if they wish to provide behavior different than throwing a ServletException..

Copyright © 2000-2008 - The Apache Software Foundation