org.apache.struts.action

Class Action

Known Direct Subclasses:
DefinitionDispatcherAction, DispatchAction, ForwardAction, IncludeAction, NoOpAction, ReloadDefinitionsAction, SwitchAction, TilesAction, ViewDefinitionsAction

public class Action
extends java.lang.Object

An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (ActionServlet) will select an appropriate Action for each request, create an instance (if necessary), and call the perform method.

Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:

When an Action instance is first created, the controller servlet will call setServlet() with a non-null argument to identify the controller servlet instance to which this Action is attached. When the controller servlet is to be shut down (or restarted), the setServlet() method will be called with a null argument, which can be used to clean up any allocated resources in use by this Action.

Version:
$Revision: 1.61 $ $Date: 2003/06/28 06:16:34 $

Authors:
Craig R. McClanahan
David Graham

Field Summary

static String
ACTION_SERVLET_KEY
Deprecated. Use Globals.ACTION_SERVLET_KEY instead.
static String
APPLICATION_KEY
Deprecated. Replaced by Globals.MODULE_KEY
static String
DATA_SOURCE_KEY
Deprecated. Replaced by Globals.DATA_SOURCE_KEY
static String
ERROR_KEY
Deprecated. Replaced by Globals.ERROR_KEY
static String
EXCEPTION_KEY
Deprecated. Replaced by Globals.EXCEPTION_KEY
static String
FORM_BEANS_KEY
Deprecated. Replaced by collection in ModuleConfig
static String
FORWARDS_KEY
Deprecated. Replaced by collection in ModuleConfig.
static String
LOCALE_KEY
Deprecated. Replaced by Globals.LOCALE_KEY
static String
MAPPINGS_KEY
Deprecated. Replaced by collection in ModuleConfig
static String
MAPPING_KEY
Deprecated. Replaced by Globals.MAPPING_KEY
static String
MESSAGES_KEY
Deprecated. Use Globals.MESSAGES_KEY instead.
static String
MESSAGE_KEY
Deprecated. Replaced by Globals.MESSAGE_KEY
static String
MULTIPART_KEY
Deprecated. Use Globals.MULTIPART_KEY instead.
static String
PLUG_INS_KEY
Deprecated. Replaced by Globals.PLUG_INS_KEY
static String
REQUEST_PROCESSOR_KEY
Deprecated. Use Globals.REQUEST_PROCESSOR_KEY instead.
static String
SERVLET_KEY
Deprecated. Use Globals.SERVLET_KEY instead.
static String
TRANSACTION_TOKEN_KEY
Deprecated. Use Globals.TRANSACTION_TOKEN_KEY instead.
protected static Locale
defaultLocale
The system default Locale.
protected ActionServlet
servlet
The controller servlet to which we are attached.
private static TokenProcessor
token
An instance of TokenProcessor to use for token functionality.

Method Summary

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), with provision for handling exceptions thrown by the business logic.
ActionForward
execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response)
Process the specified non-HTTP request, and create the corresponding non-HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic.
protected String
generateToken(HttpServletRequest request)
Generate a new transaction token, to be used for enforcing a single request for a particular transaction.
protected DataSource
getDataSource(HttpServletRequest request)
Return the default data source for the current module.
protected DataSource
getDataSource(HttpServletRequest request, String key)
Return the specified data source for the current module.
protected Locale
getLocale(HttpServletRequest request)
Return the user's currently selected Locale.
protected MessageResources
getResources()
Deprecated. This method can only return the resources for the default module.
protected MessageResources
getResources(HttpServletRequest request)
Return the default message resources for the current module.
protected MessageResources
getResources(HttpServletRequest request, String key)
Return the specified message resources for the current module.
ActionServlet
getServlet()
Return the controller servlet instance to which we are attached.
protected boolean
isCancelled(HttpServletRequest request)
Returns true if the current form's cancel button was pressed.
protected boolean
isTokenValid(HttpServletRequest request)
Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it.
protected boolean
isTokenValid(HttpServletRequest request, boolean reset)
Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it.
ActionForward
perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
Deprecated. Use the execute() method instead
ActionForward
perform(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response)
Deprecated. Use the execute() method instead
protected void
resetToken(HttpServletRequest request)
Reset the saved transaction token in the user's session.
protected void
saveErrors(HttpServletRequest request, ActionErrors errors)
Save the specified error messages keys into the appropriate request attribute for use by the <html:errors> tag, if any messages are required.
protected void
saveMessages(HttpServletRequest request, ActionMessages messages)
Save the specified messages keys into the appropriate request attribute for use by the <html:messages> tag (if messages="true" is set), if any messages are required.
protected void
saveToken(HttpServletRequest request)
Save a new transaction token in the user's current session, creating a new session if necessary.
protected void
setLocale(HttpServletRequest request, Locale locale)
Set the user's currently selected Locale.
void
setServlet(ActionServlet servlet)
Set the controller servlet instance to which we are attached (if servlet is non-null), or release any allocated resources (if servlet is null).
protected String
toHex(buffer[] )
Deprecated. This method will be removed in a release after Struts 1.1.

Field Details

ACTION_SERVLET_KEY

public static final String ACTION_SERVLET_KEY

Deprecated. Use Globals.ACTION_SERVLET_KEY instead.

The context attributes key under which our ActionServlet instance will be stored.

Since:
Struts 1.1


APPLICATION_KEY

public static final String APPLICATION_KEY

Deprecated. Replaced by Globals.MODULE_KEY

The base of the context attributes key under which our ModuleConfig data structure will be stored. This will be suffixed with the actual module prefix (including the leading "/" character) to form the actual attributes key.

For each request processed by the controller servlet, the ModuleConfig object for the module selected by the request URI currently being processed will also be exposed under this key as a request attribute.

Since:
Struts 1.1


DATA_SOURCE_KEY

public static final String DATA_SOURCE_KEY

Deprecated. Replaced by Globals.DATA_SOURCE_KEY

The context attributes key under which our default configured data source (which must implement javax.sql.DataSource) is stored, if one is configured for this module.


ERROR_KEY

public static final String ERROR_KEY

Deprecated. Replaced by Globals.ERROR_KEY

The request attributes key under which your action should store an org.apache.struts.action.ActionErrors object, if you are using the corresponding custom tag library elements.


EXCEPTION_KEY

public static final String EXCEPTION_KEY

Deprecated. Replaced by Globals.EXCEPTION_KEY

The request attributes key under which Struts custom tags might store a Throwable that caused them to report a JspException at runtime. This value can be used on an error page to provide more detailed information about what really went wrong.


FORM_BEANS_KEY

public static final String FORM_BEANS_KEY

Deprecated. Replaced by collection in ModuleConfig

The context attributes key under which our org.apache.struts.action.ActionFormBeans collection is normally stored, unless overridden when initializing our ActionServlet.


FORWARDS_KEY

public static final String FORWARDS_KEY

Deprecated. Replaced by collection in ModuleConfig.

The context attributes key under which our org.apache.struts.action.ActionForwards collection is normally stored, unless overridden when initializing our ActionServlet.


LOCALE_KEY

public static final String LOCALE_KEY

Deprecated. Replaced by Globals.LOCALE_KEY

The session attributes key under which the user's selected java.util.Locale is stored, if any. If no such attribute is found, the system default locale will be used when retrieving internationalized messages. If used, this attribute is typically set during user login processing.


MAPPINGS_KEY

public static final String MAPPINGS_KEY

Deprecated. Replaced by collection in ModuleConfig

The context attributes key under which our org.apache.struts.action.ActionMappings collection is normally stored, unless overridden when initializing our ActionServlet.


MAPPING_KEY

public static final String MAPPING_KEY

Deprecated. Replaced by Globals.MAPPING_KEY

The request attributes key under which our org.apache.struts.ActionMapping instance is passed.


MESSAGES_KEY

public static final String MESSAGES_KEY

Deprecated. Use Globals.MESSAGES_KEY instead.

The base of the context attributes key under which our module MessageResources will be stored. This will be suffixed with the actual module prefix (including the leading "/" character) to form the actual resources key.

For each request processed by the controller servlet, the MessageResources object for the module selected by the request URI currently being processed will also be exposed under this key as a request attribute.


MESSAGE_KEY

public static final String MESSAGE_KEY

Deprecated. Replaced by Globals.MESSAGE_KEY

The request attributes key under which your action should store an org.apache.struts.action.ActionMessages object, if you are using the corresponding custom tag library elements.

Since:
Struts 1.1


MULTIPART_KEY

public static final String MULTIPART_KEY

Deprecated. Use Globals.MULTIPART_KEY instead.

The request attributes key under which our multipart class is stored.


PLUG_INS_KEY

public static final String PLUG_INS_KEY

Deprecated. Replaced by Globals.PLUG_INS_KEY

The base of the context attributes key under which an array of PlugIn instances will be stored. This will be suffixed with the actual module prefix (including the leading "/" character) to form the actual attributes key.

Since:
Struts 1.1


REQUEST_PROCESSOR_KEY

public static final String REQUEST_PROCESSOR_KEY

Deprecated. Use Globals.REQUEST_PROCESSOR_KEY instead.

The base of the context attributes key under which our RequestProcessor instance will be stored. This will be suffixed with the actual module prefix (including the leading "/" character) to form the actual attributes key.

Since:
Struts 1.1


SERVLET_KEY

public static final String SERVLET_KEY

Deprecated. Use Globals.SERVLET_KEY instead.

The context attributes key under which we store the mapping defined for our controller serlet, which will be either a path-mapped pattern (/action/*) or an extension mapped pattern (*.do).


TRANSACTION_TOKEN_KEY

public static final String TRANSACTION_TOKEN_KEY

Deprecated. Use Globals.TRANSACTION_TOKEN_KEY instead.

The session attributes key under which our transaction token is stored, if it is used.


defaultLocale

protected static Locale defaultLocale
The system default Locale.


servlet

protected ActionServlet servlet
The controller servlet to which we are attached.


token

private static TokenProcessor token
An instance of TokenProcessor to use for token functionality.

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), with provision for handling exceptions thrown by the business logic. 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

Since:
Struts 1.1


execute

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             ServletRequest request,
                             ServletResponse response)
            throws Exception
Process the specified non-HTTP request, and create the corresponding non-HTTP response (or forward to another web component that will create it), with provision for handling exceptions thrown by the business logic. Return an ActionForward instance describing where and how control should be forwarded, or null if the response has already been completed.

The default implementation attempts to forward to the HTTP version of this method.

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

Since:
Struts 1.1


generateToken

protected String generateToken(HttpServletRequest request)
Generate a new transaction token, to be used for enforcing a single request for a particular transaction.

Parameters:
request - The request we are processing


getDataSource

protected DataSource getDataSource(HttpServletRequest request)
Return the default data source for the current module.

Parameters:
request - The servlet request we are processing

Since:
Struts 1.1


getDataSource

protected DataSource getDataSource(HttpServletRequest request,
                                   String key)
Return the specified data source for the current module.

Parameters:
request - The servlet request we are processing
key - The key specified in the <message-resources> element for the requested bundle

Since:
Struts 1.1


getLocale

protected Locale getLocale(HttpServletRequest request)
Return the user's currently selected Locale.

Parameters:
request - The request we are processing


getResources

protected MessageResources getResources()

Deprecated. This method can only return the resources for the default module. Use getResources(HttpServletRequest) to get the resources for the current module.

Return the message resources for the default module.


getResources

protected MessageResources getResources(HttpServletRequest request)
Return the default message resources for the current module.

Parameters:
request - The servlet request we are processing

Since:
Struts 1.1


getResources

protected MessageResources getResources(HttpServletRequest request,
                                        String key)
Return the specified message resources for the current module.

Parameters:
request - The servlet request we are processing
key - The key specified in the <message-resources> element for the requested bundle

Since:
Struts 1.1


getServlet

public ActionServlet getServlet()
Return the controller servlet instance to which we are attached.


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


isTokenValid

protected boolean isTokenValid(HttpServletRequest request)
Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it. Returns false under any of the following circumstances:
  • No session associated with this request
  • No transaction token saved in the session
  • No transaction token included as a request parameter
  • The included transaction token value does not match the transaction token in the user's session

Parameters:
request - The servlet request we are processing


isTokenValid

protected boolean isTokenValid(HttpServletRequest request,
                               boolean reset)
Return true if there is a transaction token stored in the user's current session, and the value submitted as a request parameter with this action matches it. Returns false
  • No session associated with this request
  • No transaction token saved in the session
  • No transaction token included as a request parameter
  • The included transaction token value does not match the transaction token in the user's session

Parameters:
request - The servlet request we are processing
reset - Should we reset the token after checking it?


perform

public ActionForward perform(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
            throws IOException,
                   ServletException

Deprecated. Use the execute() method instead

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


perform

public ActionForward perform(ActionMapping mapping,
                             ActionForm form,
                             ServletRequest request,
                             ServletResponse response)
            throws IOException,
                   ServletException

Deprecated. Use the execute() method instead

Process the specified non-HTTP request, and create the corresponding non-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.

The default implementation attempts to forward to the HTTP version of this method.

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


resetToken

protected void resetToken(HttpServletRequest request)
Reset the saved transaction token in the user's session. This indicates that transactional token checking will not be needed on the next request that is submitted.

Parameters:
request - The servlet request we are processing


saveErrors

protected void saveErrors(HttpServletRequest request,
                          ActionErrors errors)
Save the specified error messages keys into the appropriate request attribute for use by the <html:errors> tag, if any messages are required. Otherwise, ensure that the request attribute is not created.

Parameters:
request - The servlet request we are processing
errors - Error messages object


saveMessages

protected void saveMessages(HttpServletRequest request,
                            ActionMessages messages)
Save the specified messages keys into the appropriate request attribute for use by the <html:messages> tag (if messages="true" is set), if any messages are required. Otherwise, ensure that the request attribute is not created.

Parameters:
request - The servlet request we are processing
messages - Messages object

Since:
Struts 1.1


saveToken

protected void saveToken(HttpServletRequest request)
Save a new transaction token in the user's current session, creating a new session if necessary.

Parameters:
request - The servlet request we are processing


setLocale

protected void setLocale(HttpServletRequest request,
                         Locale locale)
Set the user's currently selected Locale.

Parameters:
request - The request we are processing
locale - The user's selected Locale to be set, or null to select the server's default Locale


setServlet

public void setServlet(ActionServlet servlet)
Set the controller servlet instance to which we are attached (if servlet is non-null), or release any allocated resources (if servlet is null).

Parameters:
servlet - The new controller servlet, if any


toHex

protected String toHex(buffer[] )

Deprecated. This method will be removed in a release after Struts 1.1.

Convert a byte array to a String of hexadecimal digits and return it.

Parameters:


Copyright B) 2000-2005 - Apache Software Foundation