Class Action
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
BaseAction,DefinitionDispatcherAction,DefinitionDispatcherAction,ReloadDefinitionsAction,ScriptAction,TilesAction,TilesAction,ViewDefinitionsAction
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 (RequestProcessor) will
select an appropriate Action for each request, create an instance (if
necessary), and call the execute 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:
- Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
- Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.
When an Action instance is first created, the controller
will call setServlet with a non-null argument to identify the
servlet instance to which this Action is attached. When the 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:
- $Rev$ $Date: 2005-08-26 21:58:39 -0400 (Fri, 26 Aug 2005) $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe action execution was a failure.static final StringThe action execution require more input in order to succeed.static final StringThe action could not execute, since the user most was not logged in.protected ActionServletThe servlet to which we are attached.static final StringThe action execution was successful. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddErrors(HttpServletRequest request, ActionMessages errors) Adds the specified errors keys into the appropriate request attribute for use by the <html:errors> tag, if any messages are required.protected voidaddMessages(HttpServletRequest request, ActionMessages messages) Adds 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.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.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 StringgenerateToken(HttpServletRequest request) Generate a new transaction token, to be used for enforcing a single request for a particular transaction.protected ActionMessagesgetErrors(HttpServletRequest request) Retrieves any existing errors placed in the request by previous actions.protected LocalegetLocale(HttpServletRequest request) Return the user's currently selected Locale.protected ActionMessagesgetMessages(HttpServletRequest request) Retrieves any existing messages placed in the request by previous actions.protected MessageResourcesgetResources(HttpServletRequest request) Return the default message resources for the current module.protected MessageResourcesgetResources(HttpServletRequest request, String key) Return the specified message resources for the current module.Return the servlet instance to which we are attached.protected booleanisCancelled(HttpServletRequest request) Returnstrueif the current form's cancel button was pressed.protected booleanisTokenValid(HttpServletRequest request) Returntrueif 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 booleanisTokenValid(HttpServletRequest request, boolean reset) Returntrueif 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 voidresetToken(HttpServletRequest request) Reset the saved transaction token in the user's session.protected voidsaveErrors(HttpServletRequest request, ActionMessages 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 voidsaveErrors(HttpSession session, ActionMessages errors) Save the specified error messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="false") or <html:errors>, if any error messages are required.protected voidsaveMessages(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 voidsaveMessages(HttpSession session, ActionMessages messages) Save the specified messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="true" is set), if any messages are required.protected voidsaveToken(HttpServletRequest request) Save a new transaction token in the user's current session, creating a new session if necessary.protected voidsetLocale(HttpServletRequest request, Locale locale) Set the user's currently selectedLocaleinto theirHttpSession.voidsetServlet(ActionServlet servlet) Set the servlet instance to which we are attached (ifservletis non-null), or release any allocated resources (ifservletis null).
-
Field Details
-
ERROR
The action execution was a failure. Show an error view, possibly asking the user to retry entering data.- Since:
- Struts 1.4
- See Also:
-
INPUT
The action execution require more input in order to succeed. This result is typically used if a form handling action has been executed so as to provide defaults for a form. The form associated with the handler should be shown to the end user.This result is also used if the given input params are invalid, meaning the user should try providing input again.
- Since:
- Struts 1.4
- See Also:
-
LOGIN
The action could not execute, since the user most was not logged in. The login view should be shown.- Since:
- Struts 1.4
- See Also:
-
SUCCESS
The action execution was successful. Show result view to the end user.- Since:
- Struts 1.4
- See Also:
-
servlet
The servlet to which we are attached.
-
-
Constructor Details
-
Action
public Action()
-
-
Method Details
-
getServlet
Return the servlet instance to which we are attached.
- Returns:
- The servlet instance to which we are attached.
-
setServlet
Set the servlet instance to which we are attached (if
servletis non-null), or release any allocated resources (ifservletis null).- Parameters:
servlet- The new controller servlet, if any
-
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
ActionForwardinstance describing where and how control should be forwarded, ornullif 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 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.- Since:
- Struts 1.1
-
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
ActionForwardinstance 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 the application business logic throws an exception- Since:
- Struts 1.1
-
addMessages
Adds 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. Initialize the attribute if it has not already been. Otherwise, ensure that the request attribute is not set.- Parameters:
request- The servlet request we are processingmessages- Messages object- Since:
- Struts 1.2.1
-
addErrors
Adds the specified errors keys into the appropriate request attribute for use by the <html:errors> tag, if any messages are required. Initialize the attribute if it has not already been. Otherwise, ensure that the request attribute is not set.- Parameters:
request- The servlet request we are processingerrors- Errors object- Since:
- Struts 1.2.1
-
generateToken
Generate a new transaction token, to be used for enforcing a single request for a particular transaction.
- Parameters:
request- The request we are processing- Returns:
- The new transaction token.
-
getErrors
Retrieves any existing errors placed in the request by previous actions. This method could be called instead of creating anew ActionMessages()at the beginning of anAction. This will prevent saveErrors() from wiping out any existing Errors- Parameters:
request- The servlet request we are processing- Returns:
- the Errors that already exist in the request, or a new ActionMessages object if empty.
- Since:
- Struts 1.2.1
-
getLocale
Return the user's currently selected Locale.
- Parameters:
request- The request we are processing- Returns:
- The user's currently selected Locale.
-
getMessages
Retrieves any existing messages placed in the request by previous actions. This method could be called instead of creating a
new ActionMessages()at the beginning of anActionThis will prevent saveMessages() from wiping out any existing Messages- Parameters:
request- The servlet request we are processing- Returns:
- the Messages that already exist in the request, or a new ActionMessages object if empty.
- Since:
- Struts 1.2.1
-
getResources
Return the default message resources for the current module.
- Parameters:
request- The servlet request we are processing- Returns:
- The default message resources for the current module.
- Since:
- Struts 1.1
-
getResources
Return the specified message resources for the current module.
- Parameters:
request- The servlet request we are processingkey- The key specified in the message-resources element for the requested bundle.- Returns:
- The specified message resource for the current module.
- Since:
- Struts 1.1
-
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.Since Action 1.3.0, the mapping for a cancellable Action must also have the new "cancellable" property set to true. If "cancellable" is not set, and the magic Cancel token is found in the request, the standard Composable Request Processor will throw an InvalidCancelException.
- Parameters:
request- The servlet request we are processing- Returns:
trueif the cancel button was pressed;falseotherwise.
-
isTokenValid
Return
trueif 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. Returnsfalseunder 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- Returns:
trueif there is a transaction token and it is valid;falseotherwise.
-
isTokenValid
Return
trueif 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. Returnsfalseunder 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 processingreset- Should we reset the token after checking it?- Returns:
trueif there is a transaction token and it is valid;falseotherwise.
-
resetToken
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
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 processingerrors- Error messages object- Since:
- Struts 1.2
-
saveMessages
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- The messages to save.nullor empty messages removes any existing ActionMessages in the request.- Since:
- Struts 1.1
-
saveMessages
Save the specified messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="true" is set), if any messages are required. Otherwise, ensure that the session attribute is not created.
- Parameters:
session- The session to save the messages in.messages- The messages to save.nullor empty messages removes any existing ActionMessages in the session.- Since:
- Struts 1.2
-
saveErrors
Save the specified error messages keys into the appropriate session attribute for use by the <html:messages> tag (if messages="false") or <html:errors>, if any error messages are required. Otherwise, ensure that the session attribute is empty.
- Parameters:
session- The session to save the error messages in.errors- The error messages to save.nullor empty messages removes any existing error ActionMessages in the session.- Since:
- Struts 1.3
-
saveToken
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
Set the user's currently selected
Localeinto theirHttpSession.- Parameters:
request- The request we are processinglocale- The user's selected Locale to be set, or null to select the server's default Locale
-