Executing actions Introduction This chapter shows you how to use the action-state element to control the execution of an action at a point within a flow. It will also show how to use the decision-state element to make a flow routing decision. Finally, several examples of invoking actions from the various points possible within a flow will be discussed. Defining action states Use the action-state element when you wish to invoke an action, then transition to another state based on the action's outcome: ]]> The full example below illustrates a interview flow that uses the action-state above to determine if more answers are needed to complete the interview: ]]> Defining decision states Use the decision-state element as an alternative to the action-state to make a routing decision using a convenient if/else syntax. The example below shows the moreAnswersNeeded state above now implemented as a decision state instead of an action-state: ]]> Action outcome event mappings Actions often invoke methods on plain Java objects. When called from action-states and decision-states, these method return values can be used to drive state transitions. Since transitions are triggered by events, a method return value must first be mapped to an Event object. The following table describes how common return value types are mapped to Event objects: Action method return value to event id mappings Method return type Mapped Event identifier expression java.lang.String the String value java.lang.Boolean yes (for true), no (for false) java.lang.Enum the Enum name any other type success
This is illustrated in the example action state below, which invokes a method that returns a boolean value: ]]>
Action implementations While writing action code as POJO logic is the most common, there are several other action implementation options. Sometimes you need to write action code that needs access to the flow context. You can always invoke a POJO and pass it the flowRequestContext as an EL variable. Alternatively, you may implement the Action interface or extend from the MultiAction base class. These options provide stronger type safety when you have a natural coupling between your action code and Spring Web Flow APIs. Examples of each of these approaches are shown below. Invoking a POJO action ]]> Invoking a custom Action implementation ]]> Invoking a MultiAction implementation ]]> Action exceptions Actions often invoke services that encapsulate complex business logic. These services may throw business exceptions that the action code should handle. Handling a business exception with a POJO action The following example invokes an action that catches a business exception, adds a error message to the context, and returns a result event identifier. The result is treated as a flow event which the calling flow can then respond to. ]]> Other Action execution examples on-start The following example shows an action that creates a new Booking object by invoking a method on a service: ]]> on-entry The following example shows a state entry action that sets the special fragments variable that causes the view-state to render a partial fragment of its view: ]]> on-exit The following example shows a state exit action that releases a lock on a record being edited: ]]> on-end The following example shows the equivalent object locking behavior using flow start and end actions: ]]> on-render The following example shows a render action that loads a list of hotels to display before the view is rendered: ]]> on-transition The following example shows a transition action adds a subflow outcome event attribute to a collection: ]]> Named actions The following example shows how to execute a chain of actions in an action-state. The name of each action becomes a qualifier for the action's result event. ]]> In this example, the flow will transition to showResults when thingTwo completes successfully.