public abstract class AbstractAction extends java.lang.Object implements Action, org.springframework.beans.factory.InitializingBean
InitializingBean
to receive an init callback when deployed within a Spring bean factory.
Event
objects such as "success" and
"error".
Modifier and Type | Field and Description |
---|---|
protected org.apache.commons.logging.Log |
logger
Logger, usable in subclasses.
|
Constructor and Description |
---|
AbstractAction() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected abstract Event |
doExecute(RequestContext context)
Template hook method subclasses should override to encapsulate their specific action execution logic.
|
protected void |
doPostExecute(RequestContext context)
Post-action execution hook, subclasses may override.
|
protected Event |
doPreExecute(RequestContext context)
Pre-action-execution hook, subclasses may override.
|
protected Event |
error()
Returns an "error" result event.
|
protected Event |
error(java.lang.Exception e)
Returns an "error" result event caused by the provided exception.
|
Event |
execute(RequestContext context)
Execute this action.
|
protected java.lang.String |
getActionNameForLogging()
Internal helper to return the name of this action for logging purposes.
|
EventFactorySupport |
getEventFactorySupport()
Returns the helper delegate for creating action execution result events.
|
protected void |
initAction()
Action initializing callback, may be overridden by subclasses to perform custom initialization logic.
|
protected Event |
no()
Returns a "no" result event.
|
protected Event |
result(boolean booleanResult)
Returns yes() if the boolean result is true, no() if false.
|
protected Event |
result(java.lang.String eventId)
Returns a result event for this action with the specified identifier.
|
protected Event |
result(java.lang.String eventId,
AttributeMap<java.lang.Object> resultAttributes)
Returns a result event for this action with the specified identifier and the specified set of attributes.
|
protected Event |
result(java.lang.String eventId,
java.lang.String resultAttributeName,
java.lang.Object resultAttributeValue)
Returns a result event for this action with the specified identifier and a single attribute.
|
protected Event |
success()
Returns a "success" result event.
|
protected Event |
success(java.lang.Object result)
Returns a "success" result event with the provided result object as a parameter.
|
protected Event |
yes()
Returns a "yes" result event.
|
protected final org.apache.commons.logging.Log logger
public EventFactorySupport getEventFactorySupport()
public void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception
protected void initAction() throws java.lang.Exception
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.
java.lang.Exception
protected Event success()
protected Event success(java.lang.Object result)
result
- the action success resultprotected Event error()
protected Event error(java.lang.Exception e)
e
- the exception that caused the error event, to be configured as an event attributeprotected Event yes()
protected Event no()
protected Event result(boolean booleanResult)
booleanResult
- the booleanprotected Event result(java.lang.String eventId)
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.
eventId
- the result event identifierprotected Event result(java.lang.String eventId, AttributeMap<java.lang.Object> resultAttributes)
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.
eventId
- the result event identifierresultAttributes
- the event attributesprotected Event result(java.lang.String eventId, java.lang.String resultAttributeName, java.lang.Object resultAttributeValue)
eventId
- the result idresultAttributeName
- the attribute nameresultAttributeValue
- the attribute valuepublic final Event execute(RequestContext context) throws java.lang.Exception
Action
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:
request scope
exist for the life of the currently
executing request only.
flash scope
exist until the next external user event
is signaled. That time includes the current request plus any redirect or additional refreshes to the next view.
flow scope
exist for the life of the flow session and
will be cleaned up automatically when the flow session ends.
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
.
execute
in interface Action
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)java.lang.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.protected java.lang.String getActionNameForLogging()
ClassUtils.getShortName(java.lang.Class)
protected Event doPreExecute(RequestContext context) throws java.lang.Exception
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
.
context
- the action execution context, for accessing and setting data in "flow scope" or "request scope"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 resultjava.lang.Exception
- an unrecoverable exception occured, either checked or uncheckedprotected abstract Event doExecute(RequestContext context) throws java.lang.Exception
context
- the action execution context, for accessing and setting data in "flow scope" or "request scope"java.lang.Exception
- an unrecoverable exception occured, either checked or uncheckedprotected void doPostExecute(RequestContext context) throws java.lang.Exception
doExecute()
was called,
e.g. when doPreExecute()
returned null
.
This implementation does nothing.
context
- the action execution context, for accessing and setting data in "flow scope" or "request scope"java.lang.Exception
- an unrecoverable exception occured, either checked or unchecked