de.matthias_burbach.strux
Class DispatcherAction

java.lang.Object
  extended byorg.apache.struts.action.Action
      extended byde.matthias_burbach.strux.DispatcherAction

public class DispatcherAction
extends org.apache.struts.action.Action

A utility class that expects to find the request parameter whose name is specified in the action mapping parameter attribute. It will return the forward that can be looked up with the value of that request parameter.

If the action mapping parameter attribute specifies a comma separated list of values they will be tried one after another until one leads to a result, e. g. if the action mapping specifies parameter="action,event" then this class will first look up a request parameter named 'action' and only then if such a request parameter does not exist it will try 'event'.

If any of the values of the action mapping parameter attribute starts with '*' then a request parameter named like the value will be searched. If it is found the part of its name (not its value!) covered by the '*' will be taken as the key to look up another parameter named like that. If such another parameter does not exist the key itself will be taken as the value. For example, if you specify parameter="*.x" and the HTTP request contains a key/value pair myaction.x=345 then 'myaction' will be used to look up the value of the parameter 'myaction'. If there is no parameter 'myaction', 'myaction' will be taken as value to forward to.

The '*.x' trick is good for two cases:

1. It can be used to forward to actions depending on a value that has been selected in a select box.

Example:

 <select name="myaction">
  <option selected value="edit">Edit</option>
  <option value="cancel">Cancel</option>
 </select>

 <input src="ok.gif" type="image" name="myaction">
 -> action = myaction because it appears as myaction.x=...(some coordinate)
 -> due to the <select> tag there is a parameter 
    'myaction' whose value is used as forward name
 

2. It can be used in forms with more than one image button where each such button leads to a different action so that the action can't be a hidden field in the form.

Example:

 <input src="ok.gif" type="image" name="ok">
 <input src="cancel.gif" type="image" name="cancel">
 -> action = ok or cancel depending on which button is pressed
    because it appears as ok.x=...(some coordinate)
 -> due to the absence of a parameter named 'ok' (or 'canceled resp.) 
    'ok' (or 'canceled resp.) is used as forward name
 

If the action mapping's parameter attribute is not specified it defaults to parameter="*.x,action".

A typical use case for this class is a HTTP form request that must be routed to one of potentially several actions and where the precise action is encoded as a request parameter (as opposed to a url path).

This class doesn't use any ActionForm but it queries the parameters directly from the request.

Author:
Matthias Burbach

Field Summary
protected static org.apache.commons.logging.Log log
          The log.
 
Fields inherited from class org.apache.struts.action.Action
ACTION_SERVLET_KEY, APPLICATION_KEY, DATA_SOURCE_KEY, defaultLocale, ERROR_KEY, EXCEPTION_KEY, FORM_BEANS_KEY, FORWARDS_KEY, LOCALE_KEY, MAPPING_KEY, MAPPINGS_KEY, MESSAGE_KEY, MESSAGES_KEY, MULTIPART_KEY, PLUG_INS_KEY, REQUEST_PROCESSOR_KEY, servlet, SERVLET_KEY, TRANSACTION_TOKEN_KEY
 
Constructor Summary
DispatcherAction()
           
 
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)
           
protected  java.lang.String findRequestParameter(javax.servlet.http.HttpServletRequest request, java.lang.String key)
          Tries to find the request parameter value for key.
 
Methods inherited from class org.apache.struts.action.Action
execute, generateToken, getDataSource, getDataSource, getLocale, getResources, getResources, getResources, getServlet, isCancelled, isTokenValid, isTokenValid, perform, perform, resetToken, saveErrors, saveMessages, saveToken, setLocale, setServlet, toHex
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
The log.

Constructor Detail

DispatcherAction

public DispatcherAction()
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
Throws:
java.lang.Exception
See Also:
Action.execute( ActionMapping, ActionForm, HttpServletRequest, HttpServletResponse)

findRequestParameter

protected java.lang.String findRequestParameter(javax.servlet.http.HttpServletRequest request,
                                                java.lang.String key)
Tries to find the request parameter value for key. If key starts with '*' the parameter value is expected to be the trailer of one of the parameter names(!). This feature was initiated to determine a dispatcher value that is encoded as an HTML image coordinate (e. g. an image button named 'ok' would transmit ok.x=345 as an HTTP request parameter).

Parameters:
request - The request to lookup the parameter in.
key - The key to find the parameter value.
Returns:
The parameter value found or null.