org.apache.struts.actions
Class MappingDispatchAction

java.lang.Object
  extended by org.apache.struts.action.Action
      extended by org.apache.struts.actions.BaseAction
          extended by org.apache.struts.actions.DispatchAction
              extended by org.apache.struts.actions.MappingDispatchAction

public class MappingDispatchAction
extends DispatchAction

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:

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.

Since:
Struts 1.2
Version:
$Rev: 480570 $ $Date: 2006-11-29 07:39:37 -0600 (Wed, 29 Nov 2006) $

Field Summary
 
Fields inherited from class org.apache.struts.actions.DispatchAction
clazz, methods, types
 
Fields inherited from class org.apache.struts.actions.BaseAction
messages
 
Fields inherited from class org.apache.struts.action.Action
servlet
 
Constructor Summary
MappingDispatchAction()
           
 
Method Summary
 org.apache.struts.action.ActionForward execute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Process the specified HTTP request, and create the corresponding HTTP response (or forward to another web component that will create it).
protected  java.lang.String getMethodName(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String parameter)
          Returns the method name, given a parameter's value.
protected  java.lang.String getParameter(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Returns the parameter value.
protected  org.apache.struts.action.ActionForward unspecified(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Method which is dispatched to when there is no value for the parameter in the ActionMapping.
 
Methods inherited from class org.apache.struts.actions.DispatchAction
cancelled, dispatchMethod, getMethod
 
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MappingDispatchAction

public MappingDispatchAction()
Method Detail

execute

public org.apache.struts.action.ActionForward execute(org.apache.struts.action.ActionMapping mapping,
                                                      org.apache.struts.action.ActionForm form,
                                                      javax.servlet.http.HttpServletRequest request,
                                                      javax.servlet.http.HttpServletResponse response)
                                               throws java.lang.Exception
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. This method dispatches the request to other methods of MappingDispatchAction using the 'parameter' attribute of ActionMapping and Java Introspection.

Overrides:
execute in class DispatchAction
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:
Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.
Throws:
java.lang.Exception - if an exception occurs

unspecified

protected org.apache.struts.action.ActionForward unspecified(org.apache.struts.action.ActionMapping mapping,
                                                             org.apache.struts.action.ActionForm form,
                                                             javax.servlet.http.HttpServletRequest request,
                                                             javax.servlet.http.HttpServletResponse response)
                                                      throws java.lang.Exception
Method which is dispatched to when there is no value for the parameter in the ActionMapping. Subclasses of MappingDispatchAction should override this method if they wish to provide default behavior different than throwing a ServletException.

Overrides:
unspecified in class DispatchAction
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:
Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.
Throws:
java.lang.Exception - if an exception occurs

getParameter

protected java.lang.String getParameter(org.apache.struts.action.ActionMapping mapping,
                                        org.apache.struts.action.ActionForm form,
                                        javax.servlet.http.HttpServletRequest request,
                                        javax.servlet.http.HttpServletResponse response)
                                 throws java.lang.Exception

Returns the parameter value.

Overrides:
getParameter in class DispatchAction
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
Throws:
java.lang.Exception - if the parameter is missing.

getMethodName

protected java.lang.String getMethodName(org.apache.struts.action.ActionMapping mapping,
                                         org.apache.struts.action.ActionForm form,
                                         javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response,
                                         java.lang.String parameter)
                                  throws java.lang.Exception
Returns the method name, given a parameter's value.

Overrides:
getMethodName in class DispatchAction
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.
Throws:
java.lang.Exception - if an error occurs
Since:
Struts 1.2.0


Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.