Class AbstractAction

java.lang.Object
org.springframework.webflow.action.AbstractAction
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean, Action
Direct Known Subclasses:
CompositeAction, EvaluateAction, ExternalRedirectAction, FlowDefinitionRedirectAction, MultiAction, RenderAction, SetAction, ViewFactoryActionAdapter

public abstract class AbstractAction extends Object implements Action, org.springframework.beans.factory.InitializingBean
Base action that provides assistance commonly needed by action implementations. This includes:
  • Implementing InitializingBean to receive an init callback when deployed within a Spring bean factory.
  • Exposing convenient event factory methods to create common result Event objects such as "success" and "error".
  • A hook for inserting action pre and post execution logic.
Author:
Keith Donald, Erwin Vervaet
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
      Logger, usable in subclasses.
  • Constructor Details

    • AbstractAction

      public AbstractAction()
  • Method Details

    • getEventFactorySupport

      public EventFactorySupport getEventFactorySupport()
      Returns the helper delegate for creating action execution result events.
      Returns:
      the event factory support
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • initAction

      protected void initAction() throws Exception
      Action initializing callback, may be overridden by subclasses to perform custom initialization logic.

      Keep in mind that this hook will only be invoked when this action is deployed in a Spring application context since it uses the Spring InitializingBean mechanism to trigger action initialisation.

      Throws:
      Exception
    • success

      protected Event success()
      Returns a "success" result event.
    • success

      protected Event success(Object result)
      Returns a "success" result event with the provided result object as a parameter.
      Parameters:
      result - the action success result
    • error

      protected Event error()
      Returns an "error" result event.
    • error

      protected Event error(Exception e)
      Returns an "error" result event caused by the provided exception.
      Parameters:
      e - the exception that caused the error event, to be configured as an event attribute
    • yes

      protected Event yes()
      Returns a "yes" result event.
    • no

      protected Event no()
      Returns a "no" result event.
    • result

      protected Event result(boolean booleanResult)
      Returns yes() if the boolean result is true, no() if false.
      Parameters:
      booleanResult - the boolean
      Returns:
      yes or no
    • result

      protected Event result(String eventId)
      Returns a result event for this action with the specified identifier. Typically called as part of return, for example:
           protected Event doExecute(RequestContext context) {
               // do some work
               if (some condition) {
                   return result("success");
               } else {
                   return result("error");
               }
           }
       
      Consider calling the error() or success() factory methods for returning common results.
      Parameters:
      eventId - the result event identifier
      Returns:
      the action result event
    • result

      protected Event result(String eventId, AttributeMap<Object> resultAttributes)
      Returns a result event for this action with the specified identifier and the specified set of attributes. Typically called as part of return, for example:
           protected Event doExecute(RequestContext context) {
               // do some work
               AttributeMap resultAttributes = new AttributeMap();
               resultAttributes.put("name", "value");
               if (some condition) {
                   return result("success", resultAttributes);
               } else {
                   return result("error", resultAttributes);
               }
           }
       
      Consider calling the error() or success() factory methods for returning common results.
      Parameters:
      eventId - the result event identifier
      resultAttributes - the event attributes
      Returns:
      the action result event
    • result

      protected Event result(String eventId, String resultAttributeName, Object resultAttributeValue)
      Returns a result event for this action with the specified identifier and a single attribute.
      Parameters:
      eventId - the result id
      resultAttributeName - the attribute name
      resultAttributeValue - the attribute value
      Returns:
      the action result event
    • execute

      public final Event execute(RequestContext context) throws Exception
      Description copied from interface: Action
      Execute this action. Action execution will occur in the context of a request associated with an active flow execution.

      Action invocation is typically triggered in a production environment by a state within a flow carrying out the execution of a flow definition. The result of action execution, a logical outcome event, can be used as grounds for a transition out of the calling state.

      Note: The RequestContext argument to this method provides access to data about the active flow execution in the context of the currently executing thread. Among other things, this allows this action to access data set by other actions, as well as set its own attributes it wishes to expose in a given scope.

      Some notes about actions and their usage of the attribute scope types:

      • Attributes set in request scope exist for the life of the currently executing request only.
      • Attributes set in flash scope exist until after view rendering is completed. That time includes the current request plus any redirect required for the view render to complete.
      • Attributes set in flow scope exist for the life of the flow session and will be cleaned up automatically when the flow session ends.
      • Attributes set in conversation scope exist for the life of the entire flow execution representing a single logical "conversation" with a user.

      All attributes present in any scope are typically exposed in a model for access by a view when an "interactive" state type such as a view state is entered.

      Note: flow scope should generally not be used as a general purpose cache, but rather as a context for data needed locally by other states of the flow this action participates in. For example, it would be inappropriate to stuff large collections of objects (like those returned to support a search results view) into flow scope. Instead, put such result collections in request scope, and ensure you execute this action again each time you wish to view those results. 2nd level caches managed outside of SWF are more general cache solutions.

      Note: as flow scoped attributes are eligible for serialization they should be Serializable.

      Specified by:
      execute in interface Action
      Parameters:
      context - the action execution context, for accessing and setting data in a scope type, as well as obtaining other flow contextual information (e.g. request context attributes and flow execution context information)
      Returns:
      a logical result outcome, used as grounds for a transition in the calling flow (e.g. "success", "error", "yes", "no", * ...)
      Throws:
      Exception - a exception occurred during action execution, either checked or unchecked; note, any recoverable exceptions should be caught within this method and an appropriate result outcome returned or be handled by the current state of the calling flow execution.
    • getActionNameForLogging

      protected String getActionNameForLogging()
      Internal helper to return the name of this action for logging purposes. Defaults to the short class name.
      See Also:
      • ClassUtils.getShortName(java.lang.Class)
    • doPreExecute

      protected Event doPreExecute(RequestContext context) throws Exception
      Pre-action-execution hook, subclasses may override. If this method returns a non-null event, the doExecute() method will not be called and the returned event will be used to select a transition to trigger in the calling action state. If this method returns null, doExecute() will be called to obtain an action result event.

      This implementation just returns null.

      Parameters:
      context - the action execution context, for accessing and setting data in "flow scope" or "request scope"
      Returns:
      the non-null action result, in which case the doExecute() will not be called, or null if the doExecute() method should be called to obtain the action result
      Throws:
      Exception - an unrecoverable exception occured, either checked or unchecked
    • doExecute

      protected abstract Event doExecute(RequestContext context) throws Exception
      Template hook method subclasses should override to encapsulate their specific action execution logic.
      Parameters:
      context - the action execution context, for accessing and setting data in "flow scope" or "request scope"
      Returns:
      the action result event
      Throws:
      Exception - an unrecoverable exception occured, either checked or unchecked
    • doPostExecute

      protected void doPostExecute(RequestContext context) throws Exception
      Post-action execution hook, subclasses may override. Will only be called if doExecute() was called, e.g. when doPreExecute() returned null.

      This implementation does nothing.

      Parameters:
      context - the action execution context, for accessing and setting data in "flow scope" or "request scope"
      Throws:
      Exception - an unrecoverable exception occured, either checked or unchecked