Class DispatchAction
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
EventDispatchAction
,LookupDispatchAction
,MappingDispatchAction
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.extras.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.
- Version:
- $Rev$ $Date$
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected Class
<? extends DispatchAction> The Class instance of thisDispatchAction
class.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.Fields inherited from class org.apache.struts.extras.actions.BaseAction
messages
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ActionForward
cancelled
(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) Method which is dispatched to when the request is a cancel button submit.protected ActionForward
dispatchMethod
(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String name) 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 Method
Introspect the current class to identify a method of the specified name that accepts the same parameter types as theexecute
method does.protected String
getMethodName
(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String parameter) Returns the method name, given a parameter's value.protected String
getParameter
(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) Returns the parameter value.protected ActionForward
unspecified
(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) Method which is dispatched to when there is no value for specified request parameter included in the request.Methods inherited from class org.apache.struts.action.Action
addErrors, addMessages, execute, generateToken, getErrors, getLocale, getMessages, getResources, getResources, getServlet, isCancelled, isTokenValid, isTokenValid, resetToken, saveErrors, saveErrors, saveMessages, saveMessages, saveToken, setLocale, setServlet
-
Field Details
-
clazz
The Class instance of thisDispatchAction
class. -
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
-
DispatchAction
public DispatchAction()
-
-
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 anActionForward
instance describing where and how control should be forwarded, ornull
if the response has already been completed.- Overrides:
execute
in classAction
- 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
null
if 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 Method which is dispatched to when there is no value for specified request parameter included in the request. Subclasses ofDispatchAction
should override this method if they wish to provide default 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
null
if 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 Method which is dispatched to when the request is a cancel button submit. Subclasses ofDispatchAction
should override this method if they wish to provide default 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
null
if the response has been completed. - Throws:
Exception
- if the application business logic throws an exception.- Since:
- Struts 1.2.0
-
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
null
if the response has been completed. - Throws:
Exception
- if the application business logic throws an exception.- Since:
- Struts 1.1
-
getParameter
protected String getParameter(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception Returns the parameter 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 creating- Returns:
- The
ActionMapping
parameter's value - Throws:
Exception
- if the parameter is missing.
-
getMethod
Introspect the current class to identify a method of the specified name that accepts the same parameter types as theexecute
method 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
-
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
- TheActionMapping
parameter's name- Returns:
- The method's name.
- Throws:
Exception
- if an error occurs.- Since:
- Struts 1.2.0
-