Class ActionDispatcher
- All Implemented Interfaces:
Serializable,Dispatcher
- Direct Known Subclasses:
EventActionDispatcher
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 toDispatchActionexcept uses "method" as a default if theparameteris 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 toDispatchAction).MAPPING_FLAVOR- uses the parameter specified in the struts-config.xml as the method name (equivalent toMappingDispatchAction).
- Since:
- Struts 1.2.7
- Version:
- $Rev$ $Date$
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ActionThe associated Action to dispatch to.The Class instance of thisDispatchActionclass.static final intIndicates "default" dispatch flavor.static final intIndicates flavor compatible with DispatchAction.protected intIndicates dispatch flavor.static final intIndicates "mapping" dispatch flavor.protected static MessageResourcesThe message resources for this package.The set of Method objects we have introspected for this class, keyed by method name.protected Class<?>[]The set of argument type classes for the reflected method call. -
Constructor Summary
ConstructorsConstructorDescriptionActionDispatcher(Action actionInstance) Construct an instance of this class from the supplied parameters.ActionDispatcher(Action actionInstance, int flavor) Construct an instance of this class from the supplied parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected ActionForwardcancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) Dispatches to the target class' cancelled method, if present, otherwise returns null.dispatch(ActionContext context) Dispatches to the action referenced by the specified context.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.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).protected MethodIntrospect the current class to identify a method of the specified name that accepts the same parameter types as theexecutemethod 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 selectedflavorspecified for thisActionDispatcher.protected booleanisCancelled(HttpServletRequest request) Returnstrueif the current form's cancel button was pressed.protected ActionForwardunspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) Dispatches to the target class'unspecifiedmethod, if present, otherwise throws a ServletException.
-
Field Details
-
DEFAULT_FLAVOR
public static final int DEFAULT_FLAVORIndicates "default" dispatch flavor.- See Also:
-
MAPPING_FLAVOR
public static final int MAPPING_FLAVORIndicates "mapping" dispatch flavor.- See Also:
-
DISPATCH_FLAVOR
public static final int DISPATCH_FLAVORIndicates flavor compatible with DispatchAction.- See Also:
-
messages
The message resources for this package. -
actionInstance
The associated Action to dispatch to. -
flavor
protected int flavorIndicates dispatch flavor. -
clazz
The Class instance of thisDispatchActionclass. -
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. -
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 Details
-
ActionDispatcher
Construct an instance of this class from the supplied parameters.- Parameters:
actionInstance- The action instance to be invoked.
-
ActionDispatcher
Construct an instance of this class from the supplied parameters.- Parameters:
actionInstance- The action instance to be invoked.flavor- The flavor of dispatch to use.
-
-
Method Details
-
execute
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it). Return anActionForwardinstance describing where and how control should be forwarded, ornullif the response has already been completed.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The HTTP request we are processingresponse- The HTTP response we are creating- Returns:
- The forward to which control should be transferred, or
nullif the response has been completed. - Throws:
Exception- if an exception occurs
-
unspecified
protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception Dispatches to the target class'
unspecifiedmethod, if present, otherwise throws a ServletException. Classes utilizingActionDispatchershould provide anunspecifiedmethod if they wish to provide behavior different than throwing a ServletException.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The non-HTTP request we are processingresponse- The non-HTTP response we are creating- Returns:
- The forward to which control should be transferred, or
nullif the response has been completed. - Throws:
Exception- if the application business logic throws an exception.
-
cancelled
protected ActionForward cancelled(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception Dispatches to the target class' cancelled method, if present, otherwise returns null. Classes utilizing
ActionDispatchershould provide acancelledmethod if they wish to provide behavior different than returning null.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The non-HTTP request we are processingresponse- The non-HTTP response we are creating- Returns:
- The forward to which control should be transferred, or
nullif the response has been completed. - Throws:
Exception- if the application business logic throws an exception.
-
dispatchMethod
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name) throws Exception Dispatch to the specified method.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The non-HTTP request we are processingresponse- The non-HTTP response we are creatingname- The name of the method to invoke- Returns:
- The forward to which control should be transferred, or
nullif the response has been completed. - Throws:
Exception- if the application business logic throws an exception.
-
dispatchMethod
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name, Method method) throws Exception Dispatch to the specified method.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The non-HTTP request we are processingresponse- The non-HTTP response we are creatingname- The name of the method to invokemethod- The method to invoke- Returns:
- The forward to which control should be transferred, or
nullif the response has been completed. - Throws:
Exception- if the application business logic throws an exception.
-
getMethod
Introspect the current class to identify a method of the specified name that accepts the same parameter types as theexecutemethod does.- Parameters:
name- Name of the method to be introspected- Returns:
- The method with the specified name.
- Throws:
NoSuchMethodException- if no such method can be found
-
getParameter
protected String getParameter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception Returns the parameter value as influenced by the selected
flavorspecified for thisActionDispatcher.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The HTTP request we are processingresponse- The HTTP response we are creating- Returns:
- The
ActionMappingparameter's value - Throws:
Exception- if an error occurs.
-
getMethodName
protected String getMethodName(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String parameter) throws Exception Returns the method name, given a parameter's value.- Parameters:
mapping- The ActionMapping used to select this instanceform- The optional ActionForm bean for this request (if any)request- The HTTP request we are processingresponse- The HTTP response we are creatingparameter- TheActionMappingparameter's name- Returns:
- The method's name.
- Throws:
Exception- if an error occurs.
-
isCancelled
Returns
trueif the current form's cancel button was pressed. This method will check if theGlobals.CANCEL_KEYrequest attribute has been set, which normally occurs if the cancel button generated by CancelTag was pressed by the user in the current request. Iftrue, validation performed by an ActionForm'svalidate()method will have been skipped by the controller servlet.- Parameters:
request- The servlet request we are processing- Returns:
trueif the current form's cancel button was pressed;falseotherwise.- See Also:
-
dispatch
Description copied from interface:DispatcherDispatches to the action referenced by the specified context.- Specified by:
dispatchin interfaceDispatcher- Parameters:
context- the current action context- Returns:
- the result type,
nullif the response was handled directly, orVoidif the executed method has no return signature - Throws:
Exception- if an exception occurs- Since:
- Struts 1.4
-