Chapter 2. Flow definition

2.1. Introduction


Spring Web Flow allows developers to build reusable, self-contained controller modules called flows. A flow defines a user dialog that responds to user events to drive the execution of application code to complete a business goal.

Flows are defined declaratively using a rich domain-specific language (DSL) tailored to the problem domain of UI flow. Currently, XML and Java-based forms of this language are provided.

This chapter documents Spring Web Flow's core flow definition language. You will learn the core domain constructs of the system and how those constructs are representable in an externalized XML form.

2.2. FlowDefinition

A flow definition is a instance of org.springframework.webflow.definition.FlowDefinition. This is the central domain artifact representing the definition of a user dialog or task.

A flow definition consists of a set of one or more states, where each state defines a step in the flow that when entered executes a behavior. What behavior is executed is a function of the state's type and configuration. The outcome of a state's execution, called an event, is used by the flow to drive a state transition.

Exactly one of a flow's states is the startState that defines the starting point of the flow. Optionally, a flow can have one or more end states defining the ending points of the flow.

An example definition of a simple flow to carry out a search process is shown graphically below:

Search Flow

The default FlowDefinition implementation in Spring Web Flow is org.springframework.webflow.engine.Flow. Its configurable properties are summarized below:

Table 2.1. Flow properties

Property nameDescriptionCardinalityDefault value
idThe identifier of the flow definition, typically unique to all other flows of the application.1 
attributesAdditional custom attributes about the flow.0..*None
statesThe steps of the flow.1..* 
startStateThe starting point of the flow.1 
variablesThe set of flow variables to create each time an execution of the flow is started.0..*Empty
inputMapper The service responsible for mapping flow input provided by a caller each time an execution of the flow is started. 0..1Null
startActionsThe list of actions to execute each time an execution of the flow is started.0..*Empty
endActionsThe list of actions to execute each time an execution of the flow ends.0..*Empty
outputMapper The service responsible for mapping flow output to expose to the caller each time an execution of the flow ends. 0..1Null
globalTransitionsThe set of transitions shared by all states of the flow.0..*Empty
exceptionHandlersAn ordered set of handlers to be applied when an exception is thrown within a state of the flow.0..*Empty
inlineFlowsA set of inner flows that will be called as subflows; these flows are locally scoped to the outer flow.0..*Empty

Below is a high level example of how these properties can be configured in XML form or directly in Java code.

2.2.1. XML-based Flow template

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <attribute .../>

        <var .../>

        <input-mapper .../>

        <start-actions>
            ...
        </start-actions>

        <start-state idref="yourStartingStateId"/>

        <-- your state definitions go here -->

        <global-transitions>
            ...
        </global-transitions>

        <end-actions>
            ...
        </end-actions>

        <output-mapper .../>

        <exception-handler .../>

        <inline-flow>
            ...
        </inline-flow>

    </flow>
         	

2.2.2. Java Flow API example

    Flow flow = new Flow("id");
    flow.getAttributeMap().put("name", "value");
    flow.addState(...);
    flow.setStartState("startingPoint");
    flow.addVariable(...);
    flow.setInputMapper(...);
    flow.getStartActionList().add(...);
    flow.getEndActionList().add(...);
    flow.setOutputMapper(...);
    flow.getGlobalTransitionSet().add(...);
    flow.getExceptionHandlerSet().add(...);
    flow.addInlineFlow(...);
          

A Flow is typically built by a FlowBuilder rather than assembled by hand. The flow building subsystem is contained within the org.springframework.webflow.engine.builder package. The XML Flow Builder and spring-webflow.xsd schema are located within the org.springframework.webflow.engine.builder.xml package. The XML-based format is the most popular way to define flows.

2.3. StateDefinition

A StateDefinition defines the behavior for a step of a FlowDefinition. The base implementation class for all Flow state types is org.springframework.webflow.engine.State. This abstract class defines common properties applicable to all state types, which include:

Table 2.2. State properties

Property nameDescriptionCardinalityDefault value
idThe id of the state, unique to its containing flow definition.1 
ownerThe owning flow definition.1 
attributesAdditional custom attributes about the state.0..*None
entryActionsThe list of actions to execute each time the state is entered.0..*Empty
exceptionHandlersAn ordered set of handlers to be invoked when an exception is thrown within the state.0..*Empty

2.4. Transitionable State

A central subclass of State is org.springframework.webflow.TransitionableState. This abstract class defines common properties applicable to all state types that execute transitions to other states in response to events. These properties include:

Table 2.3. TransitionableState properties

Property nameDescriptionCardinalityDefault value
transitionsThe eligible paths out of this state.1..* 
exitActionsThe list of actions to execute each time this state is exited.0..*Empty

Below is a mock flow definition snippet showing how properties may be configured for a TransitionableState in XML and in Java code:

2.4.1. XML-based state template

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="myStateId"/>

        <xxx-state id="myStateId">
            <attribute name="..." value="..."/>

            <entry-actions>
                ...
            </entry-actions>

            <transition on="..." to="..."/>
            <transition on-exception="..." to="..."/>

            <exit-actions>
    	        ...
            </exit-actions>

            <exception-handler .../>
        </xxx-state>

    </flow>
         	

2.4.2. Java state API example

    Flow flow = new Flow("id");
    TransitionableState state = new XXXState(flow, "stateId");
    state.getAttributeMap().put("name", "value");
    state.getEntryActionList().add(...);
    state.getTransitionSet().add(...);
    state.getExitActionList().add(...);
          

A State is typically constructed by a FlowArtifactFactory, used by a FlowBuilder during flow assembly. The flow building subsystem is contained within the org.springframework.webflow.engine.builder package.

2.5. TransitionDefinition

A transition takes a flow from one state to another, defining a path through the flow. This is modeled using a TransitionDefinition.

Recall that all TransitionableStates have a set of one or more transitions, each defining a path to another state in the flow (or a recursive path back to the same state). When a transitionable state is entered it executes a behavior. For example, a transitionable state called "Display Form" may display a form to the user and wait for user input. The outcome of the state's execution, called an event, is used to drive execution of one of the state's transitions. For example, the user may press the form submit button which signals a submit event that matches the transition to the "Process Submit" state.

This event-driven transition execution process is shown graphically below:

Transition execution

The transition definition implementation is defined by an instance of org.springframework.webflow.engine.Transition. Its properties are summarized below:

Table 2.4. Transition properties

Property nameDescriptionCardinalityDefault value
attributesAdditional attributes describing the transition.0..*None
matchingCriteriaThe strategy that determines if the transition matches on an event occurrence.1Always matches
executionCriteriaThe strategy that determines if the transition, once matched, is allowed to execute.1Always allowed
targetStateResolver The strategy that resolves the target state of the transition. Most transitions always resolve to the same target state. This strategy allows for dynamic resolution. 1 

Below is a high-level example of how a Transition can be configured in XML form or directly in Java code.

2.5.1. Transition XML template

    <transition on="event" to="targetState">
        <attribute ... />
        <action ... />
    </transition>
         	

2.5.2. Transition Java API example

    Transition transition = new Transition("targetState");
    transition.getAttributeMap().put("name", "value");
    transition.setMatchingCriteria(new EventIdTransitionCriteria("event"));
    transition.setExecutionCriteria(...);
         	

2.5.3. Action transition execution criteria

In the XML transition template above note the support for the action element within the transition element.

A transition may be configured with one or more actions that execute before the transition itself executes as executionCriteria. If one or more of these actions do not complete successfully the transition will not be allowed. This action transition criteria makes it possible to execute arbitrary logic after a transition is matched but before it is executed. This is useful when you want to execute event post-processing logic. A good example is executing form data binding and validation behavior after a form submit event.

2.5.4. Dynamic transitions

A transition's target state resolver can be configured to dynamically calculate the target state. For example:

    <transition on="back" to="${flowScope.lastStateId}" />
         	

This will transition the flow to the state resolved by evaluating the flowScope.lastStateId expression.

2.5.5. Global transitions

As outlined, one or more transitions are added to all TransitionableState types, attached at the state-level. Optionally, transitions may also be added at the flow-level where they are shared by all states. These shared transitions are called global transitions.

When an event is signaled in a transitionable state the state will first try and match one of its own transitions. If there is no match at the state level the set of global transitions will be tested. If there still is no match a NoMatchingTransitionException will be thrown.

Global transitions are useful in situations where many states of the flow share the same transitional criteria. For example, consider a navigation menu that displays alongside each view of a flow. Logic to process navigation menu events is needed by all view states. This is the problem global transitions are designed to solve.

2.5.5.1. Global transitions - XML example

The following example shows transitions defined at the state level, as well as global transitions defined at the flow level.

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="state1"/>

        <xxx-state id="state1">
            <transition on="localEvent1" to="state2"/>
        </xxx-state>

        <xxx-state id="state2">
            <transition on="localEvent1" to="state1"/>
        </xxx-state>

        <global-transitions>
            <transition on="globalEvent1" to="state1"/>
            <transition on="globalEvent2" to="state2"/>
        </global-transitions>

    </flow>
    			

In this mock example state1 defines one transition and also inherits the two others defined within the global-transitions element. Any other states defined within this flow would also inherit those global transitions.

This example is shown graphically below:

Global transitions

2.5.6. Transition executing state exception handlers

The <transition/> element contains an exclusive on-exception attribute used to specify an exception-based criteria for transition execution. This allows you to transition the flow to another state on the occurrence of an exception.

2.5.6.1. Exception handling - XML example

The following example shows a transition that is applied as a state exception handler:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="state1"/>

        <xxx-state id="state1">
            <transition on="event1" to="state2"/>
            <transition on-exception="example.MyBusinessException" to="state3"/>
        </xxx-state>

        ...

    </flow>
    			

In this example state1 defines one transition and an exception handler which executes a transition to state3 if a MyBusinessException is thrown within the state.

2.6. Concrete state types

Spring Web Flow has five (5) built-in concrete state types, all contained within the org.springframework.webflow.engine package. These states execute common controller behaviors including:

  1. allowing the user to participate in a flow (ViewState)

  2. executing business application code (ActionState)

  3. making a flow routing decision (DecisionState)

  4. spawning another flow as a subflow (SubflowState)

  5. terminating a flow (EndState)

Each of these state types, with the exception of EndState, is transitionable. This hierarchy is illustrated below:

FlowDefinition class diagram

As you will see, with these five basic state types you can develop rich controller modules.

2.6.1. ViewState

When entered a view state allows the user (or other external client) to participate in a flow. This participation process goes as follows:

  1. The entered view state makes a org.springframework.webflow.execution.ViewSelection that represents a logical response to issue to the caller.

  2. The flow execution 'pauses' in this state, and control is returned to the calling system.

  3. The calling system uses the returned ViewSelection to present a suitable interface (or other response) to the user.

  4. After some 'think time' the user signals an input event to resume the flow execution from the 'paused' point.

Spring Web Flow gives you full control over the view selection process and, on resume, how a view state responds to a user input event. It's important to understand that Spring Web Flow is not responsible for response rendering--as a controller, a flow makes a logical view selection when user input is required, where a view selection serves as a response instruction. It is up to the calling system to interpret that instruction to issue a response suitable for the environment in which the flow is executing.

The properties of a org.springframework.webflow.engine.ViewState are summarized below:

Table 2.5. ViewState properties

Property nameDescriptionCardinalityDefault value
viewSelectorThe strategy that makes the view selection when this state is entered.0..1Null
renderActions The list of actions to execute each time a renderable view selection is made. Allows for execution of pre-render logic. 0..*Empty

The org.springframework.webflow.execution.ViewSelection base class is abstract, acting as a marker indicating a response should be issued to the client interacting with the flow. Concrete subtypes exist for each of the supported response types. These response types are summarized below:

Table 2.6. Concrete ViewSelection types

TypeDescription
ApplicationViewRequests the rendering of a local, internal application view resource such as a JSP, Velocity, or Freemarker template.
FlowExecutionRedirect Requests a redirect back to the ViewState at a unique flow execution URL. When this URL is accessed on subsequent requests an ApplicationView will be reconstituted and rendered. The URL is refreshable while the flow execution remains active.
[Note]Note

Multiple flow execution URLs may be generated for a single logical user conversation. In that case each flow execution URL provides access to the conversation from a previous point (ViewState). Accessing the URL refreshes the execution from that point.

FlowDefinitionRedirect Requests a redirect that launches an entirely new flow execution. Used to support redirect to flow (flow chaining) and restart flow use cases.
ExternalRedirect Requests a redirect to an arbitrary external URL, typically used to inteface with an external system.
NullView Requests that no response be issued; for use in corner cases where the flow itself has already issued the response.

2.6.1.1. ViewSelector

The creational strategy responsible for making a ViewSelection when an ViewState is entered is org.springframework.webflow.engine.ViewSelector. This provides a plugin-point for customizing how a response instruction is constructed.

Four ViewSelector implementations are provided with Spring Web Flow:

Table 2.7. ViewSelector implementations

ImplementationDescription
ApplicationViewSelector Returns an ApplicationView referencing a logical viewName to render and containing a modelMap with the application data needed by the rendering process (by default, this map contains the union of the data scopes such flow, flash, and request scope). Supports setting a redirect flag that triggers a browser redirect to the selected view using a FlowExecutionRedirect. The default implementation.
FlowDefinitionRedirectSelector Returns a FlowDefinitionRedirect with a flowId and executionInput map requesting the launch of an entirely new flow execution (an instance of the FlowDefinition identified by the flowId). Useful for redirect after flow completion, where one flow ending should trigger the start of another flow independently.
ExternalRedirectSelector Returns an ExternalRedirect that triggers a browser redirect to an abitrary external URL. Mainly used by end states to redirect to external systems after flow completion, but can also be used by view states to interface with an external system that may call back into the flow execution at a later point.
NullViewSelector Returns an NullView indicating that no response should be issued.

2.6.1.2. ViewState class diagram

The class diagram below shows the ViewState and the associated types used to carry out the view selection process:

ViewState class diagram

2.6.1.3. ViewState XML - application view selection

The following example shows a view-state definition in XML that makes an application view selection when entered, selecting the searchForm view for display and, on resume, responding to two possible user input events (submit and cancel) in different ways:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="displaySearchForm"/>

        <view-state id="displaySearchForm" view="searchForm">
            <transition on="submit" to="processFormSubmission"/>
            <transition on="cancel" to="processCancellation"/>
        </view-state>

        ...

    </flow>
    			

View name expressions may also be specified for the view attribute to achieve runtime view name calculation. For example, view="${requestScope.calculatedViewName}".

2.6.1.4. ViewState API - application view selection

The following example shows the equivalent view state definition using the FlowBuilder API:

    public class SearchFlowBuilder extends AbstractFlowBuilder {
    	public void buildStates() {
    	    addViewState("displaySearchForm", "searchForm",
    	        new Transition[] {
    	            transition(on("submit"), to("processFormSubmission")),
    	            transition(on("cancel"), to("processFormCancellation"))
    	        }
    	    );
            ...
        }
    }
    			

2.6.1.5. ViewState XML - flow execution redirect

The following example illustrates a view-state definition in XML that makes an flow execution redirect selection when entered, redirecting to the yourList view for display.

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="displayList"/>

        <view-state id="displayList" view="redirect:yourList">
            <transition on="add" to="addListItem"/>
        </view-state>

        ...

    </flow>
    			

This example is called a flow execution redirect because the application view selected is rendered only after a redirect to the flow execution. The redirect request is sent to a URL that refreshes the flow execution paused in the displayList view state. Refresh then triggers the rendering of the yourList application view on the next request into the server.

2.6.1.5.1. POST+REDIRECT+GET in Spring Web Flow

The above example is one way to achieve the POST+REDIRECT+GET pattern in Spring Web Flow. When the redirect is performed, the GET request issued hits a stable flow execution URL which remains active for the duration of the conversation. This URL may be freely refreshed. Browser navigational buttons may be used freely without browser warnings.

Later in this document the execution attribute alwaysRedirectOnPause is discussed, which enforces this pattern by default. In that case each time a view state is entered a redirect is always performed--automatically.

2.6.1.6. ViewState API - flow execution redirect

The following example shows the equivalent view state definition using the FlowBuilder API:

    public class SearchFlowBuilder extends AbstractFlowBuilder {
    	public void buildStates() {
    	    addViewState("displayList", viewSelector("redirect:yourView"),
   	            transition(on("add"), to("addListItem"))
    	    );
            ...
        }
    }
    			

2.6.1.7. ViewState XML - null view

The following example illustrates a view-state definition in XML that makes a null view selection when entered, which causes no additional response to be issued.

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="displayPdf"/>

        <view-state id="displayPdf">
            <render-actions>
                <action bean="pdfWriter" method="write"/>
            </render-actions>
        </view-state>

        ...

    </flow>
    			

2.6.1.8. FlowDefinitionRedirect and ExternalRedirect

The FlowDefinitionRedirect and ExternalRedirect are not normally used with a view state. Instead they're used in an end state to continue with another, independent flow or to redirect to an external URL. Examples are provided in the discussion of the end state.

2.6.1.9. ViewState XML - form state behavior

The following example illustrates a view-state definition in XML that encapsulates typical "form state" behavior.

Consider the requirements of typical input forms. Most forms require pre-render or setup logic to execute before the form is displayed. For example, such logic might load the backing form object from the database, install formatters for formatting form field values, and pull in supporting form data needed to populate drop-down menus.

In addition, most forms require post-back or submission logic to execute when the form is submitted. This logic typically involves binding form input to the backing form object and performing type conversion and data validation.

This "form state" behavior of form setup, display, and post-back is handled elegantly in Spring Web Flow by the capabilities of the view-state construct. See below:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="displayForm"/>

        <view-state id="displayForm" view="form">
            <render-actions>
                <action bean="formAction" method="setupForm"/>
                <action bean="formAction" method="loadFormReferenceData"/>
            </render-actions>
            <transition on="submit" to="saveForm">
                <action bean="formAction" method="bindAndValidate"/>
            </transition>
        </view-state>

        ...

    </flow>
    			

This reads "when this flow starts enter the displayForm state to execute the setupForm and loadFormReferenceData methods before rendering the form view. On submit, transition to the saveForm state if the bindAndValidate method executes successfully."

2.6.2. ActionState

When entered, an action state executes business application code, then responds to the result of that execution by deciding what state in the flow to enter next. Specifically:

  1. The entered action state executes an ordered list of one or more org.springframework.webflow.execution.Action instances. This Action interface is the central abstraction that encapsulates the execution of a logical unit of application code.

  2. The state determines if the outcome of the first action's execution matches a transition. If there is a match, the transition is executed. If there is no match, the next action in the list is executed. This process continues until a transition is matched or the list of actions is exhausted.

Spring Web Flow gives you full control over implementing your own actions and configuring when they should be invoked within the lifecycle of a flow. The system can also automatically adapt methods on your existing application objects (POJOs) to the Action interface in a non-invasive manner. This means in many cases you can implement your flows without needing to develop custom glue code to bind SWF to your service layer operations.

The properties of a org.springframework.webflow.engine.ActionState are summarized below:

Table 2.8. ActionState properties

Property nameDescriptionCardinalityDefault value
actionsThe ordered list of actions to execute when the state is entered.1..* 

2.6.2.1. Action execution points

As outlined, the ActionState is the dedicated state type for invoking one or more actions and responding to their result to drive a state transition. There are also other points within the lifecycle of a flow where a chain of actions can be executed. At all of these points the only requirement is that these actions implement the central org.springframework.webflow.execution.Action interface.

Table 2.9.  Other points in a Flow where an Action can be executed and how those points can be defined in a XML-based Flow definition.

PointDescriptionXML Configuration Element
on flow start Each time a new flow session starts. A flow's <start-actions/>
on state entry Each time a state enters. A state's <entry-actions/>
on transition Each time a state transition is matched but before it is executed. A transition <action/>
on state exit Each time a transitionable state exits. A transitionable state's <exit-actions/>
before view rendering Each time a renderable view selection is made. A view state's <render-actions/>
on flow end Each time a flow session terminates. A flow's <end-actions/>

[Note]Note
The above other points in a flow where actions may be executed do not allow you to execute a state transition in response to the action result event. If you need such flow control you must execute the action from within an action state.

2.6.2.2. Action attributes

An Action may be annotated with attributes by wrapping the Action in a decorator, an instance of org.springframework.webflow.engine.AnnotatedAction. These attributes may provide descriptive characteristics, or may be used to affect the action's execution in a specific usage context.

Support for setting several common attributes is provided for convenience. These include:

Table 2.10. Common Action attributes

Attribute nameDescription
caption A short description about the action, suitable for display as a tooltip.
description A long description about the action, suitable for display in a text box.
name The name of the action, used to qualify the action's result event. For example, an Action named placeOrder that returns success would be assigned a result event identified by placeOrder.success. This allows you to distinguish logical execution outcomes by action, useful when invoking multiple actions as part of a chain.
method The name of the target method on the Action instance to invoke to carry out execution. This facilitates multiple action methods per Action instance, supported by the org.springframework.webflow.action.MultiAction.

2.6.2.3. ActionState class diagram

The class diagram below shows the ActionState and the associated types used to carry out the action execution process:

ActionState class diagram

2.6.2.4. ActionState XML - simple action execution

The following example constructs an ActionState definition from XML that executes a single action when entered and then responds to its result:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="executeSearch"/>

        <action-state id="executeSearch">
            <action bean="searchAction"/>
            <transition on="success" to="displayResults"/>
        </action-state>

        ...

    </flow>
    			

This state definition reads "when the executeSearch state is entered, execute the searchAction. On successful execution, transition to the displayResults state."

The binding between the searchAction id and an Action implementation is made at Flow build time by querying a service locator, typically a Spring BeanFactory. For example:

    <beans>
        <bean id="searchAction" class="example.webflow.SearchAction"/>
    </beans>
    			

... binds the searchAction action identifier to a singleton instance of the example.webflow.SearchAction class.

A simple SearchAction implementation might look like this:

    public class SearchAction implements Action {
        private SearchService searchService;

        public SearchAction(SearchService searchService) {
            this.searchService = searchService;
        }

        public Event execute(RequestContext context) {
            // lookup the search criteria in "flow scope"
            SearchCriteria criteria =
                (SearchCriteria)context.getFlowScope().get("criteria");

            // execute the search
            Collection results = searchService.executeSearch(criteria);

            // set the results in "request scope"
            context.getRequestScope().put("results", results);

            // return "success"
            return new Event(this, "success");
        }
    }
    			

2.6.2.5. ActionState API - standard action

The following example constructs the equivalent action state definition using the FlowBuilder API:

    public class SearchFlowBuilder extends AbstractFlowBuilder {
    	public void buildStates() {
            ...
            addActionState("executeSearch", action("searchAction"),
                transition(on("success"), to("displayResults")));
            ...
        }
    }
    			

2.6.2.6. ActionState XML - multi action

The next example constructs an ActionState definition from XML that executes a single action method on a org.springframework.webflow.action.MultiAction and then responds to its result:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="executeSearch"/>

        <action-state id="executeSearch">
            <action bean="searchAction" method="executeSearch"/>
            <transition on="success" to="displayResults"/>
        </action-state>

        ...

    </flow>
    			

This state definition reads "when the executeSearch state is entered, call the executeSearch method on the searchFlowAction. On successful execution, transition to the displayResults state."

A SearchAction implementation containing multiple action methods might look like this:

    public class SearchAction extends MultiAction {
        private SearchService searchService;

        public SearchAction(SearchService searchService) {
            this.searchService = searchService;
        }

        public Event executeSearch(RequestContext context) {
            // lookup the search criteria in "flow scope"
            SearchCriteria criteria =
                (SearchCriteria)context.getFlowScope().get("criteria");

            // execute the search
            Collection results = searchService.executeSearch(criteria);

            // set the results in "request scope"
            context.getRequestScope().put("results", results);

            // return "success"
            return success();
        }

        public Event someOtherRelatedActionMethod(RequestContext context) {
            ...
            return success();
        }

        public Event yetAnotherRelatedActionMethod(RequestContext context) {
            ...
            return success();
        }
    }
    			

As you can see, this allows you to define one to many action methods per Action class. With this approach, there are two requirements:

  1. Your Action class must extend from org.springframework.webflow.MultiAction, or another class that extends from MultiAction. The multi action cares for the action method dispatch that is based on the value of the method property.

  2. Each action method must conform to the signature illustrated above: public Event ${method}(RequestContext) { ... }

MultiActions are useful for centralizing command logic on a per-flow definition basis, as a flow definition typically carries out execution of a single application use case.

2.6.2.7. ActionState API - multi action

The following example constructs the equivalent action state definition using the FlowBuilder API:

    public class SearchFlowBuilder extends AbstractFlowBuilder {
    	public void buildStates() {
            ...
            addActionState("executeSearch", invoke("executeSearch", action("searchAction")),
                transition(on("success"), to("displayResults")));
            ...
        }
    }
    			

2.6.2.8. ActionState XML - bean action

The next example constructs an ActionState definition from XML that executes a single method on a Plain Old Java Object (POJO) and then responds to the result:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <start-state idref="executeSearch"/>

        <action-state id="executeSearch">
            <bean-action bean="searchService" method="executeSearch">
                <method-arguments>
                    <argument expression="${flowScope.criteria}"/>
                </method-arguments>
                <method-result name="results"/>
            </bean-action>
            <transition on="success" to="displayResults"/>
        </action-state>

        ...

    </flow>
    			

This state definition reads "when the executeSearch state is entered, call the executeSearch method on the searchService passing it the object indexed by name criteria in flowScope. On successful execution, expose the method return value in the default scope (request) under the name results and transition to the displayResults state."

In this example the referenced bean searchService would be your application object, typically a transactional business service. Such a service implementation must have defined the the Collection executeSearch(SearchCriteria) method, typically by implementing a service interface:

    public interface SearchService {
        public Collection executeSearch(SearchCriteria criteria);
    }
    			

With this approach there are no requirements on the signature of the methods that carry out action execution, nor is there any requirement to extend from a Web Flow specific base class. Basically, you are not required to write a custom Action implementation at all--you simply instruct Spring Web Flow to call your business methods directly. The need for custom "glue code" to bind your web-tier to your middle-tier is eliminated.

Spring Web Flow achieves this by automatically adapting the method on your existing application object to the Action interface and caring for exposing any return value in the correct scope.

This adaption process is shown graphically below:

Bean->Action adapter

2.6.2.9. ActionState XML - decision bean action

The following example constructs an ActionState from XML that executes an action whose execution result forms the basis for the transition decision:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <action-state id="shippingRequired">
            <bean-action bean="shippingService" method="isShippingRequired">
                <method-arguments>
                    <argument expression="${flowScope.purchase}"/>
                </method-arguments>
            </bean-action>
            <transition on="yes" to="enterShippingDetails"/>
            <transition on="no" to="placeOrder"/>
        </action-state>

        ...

    </flow>
    			

This state definition reads "if the isShippingRequired method on the shippingService returns true, transition to the enterShippingDetails state, otherwise transition to the placeOrder state."

[Note]Note

Note how the boolean return value of the isShippingRequired method is converted to the event identifiers yes or no.

This conversion process is handled by the action adapter responsible for adapting the method on your application object to the org.springframework.webflow.execution.Action interface. By default, this adapter applies a number of rules for creating a result event from a method return value.

These conversion rules are:

Table 2.11. Default method return value to Event conversion rules

Return typeEvent identifier
booleanyes or no
java.lang.Enumthis.name()
org.springframework.core.enum.LabeledEnumthis.getLabel()
org.springframework.webflow.execution.Eventthis.getId()
java.lang.Stringthe string
any other typesuccess

You may customize these default conversion policies by setting a custom ResultEventFactory instance on the bean invoking action performing the adaption. Consult the JavaDoc documentation for more details on how to do this.

2.6.2.10. ActionState XML - decision bean action with enum return value

The following example constructs an ActionState from XML that executes a action that invokes a method on an application object that returns a java.lang.Enum:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <action-state id="shippingRequired">
            <bean-action bean="shippingService" method="calculateShippingMethod"/>
            	<method-arguments>
            	    <argument expression="${flowScope.order}"/>
            	</method-arguments>
            </bean-action>
            <transition on="BASIC" to="enterBasicShippingDetails"/>
            <transition on="EXPRESS" to="enterExpressShippingDetails"/>
            <transition on="NONE" to="placeOrder"/>
        </action-state>

        ...

    </flow>
    			

This state definition reads "if the calculateShippingMethod method on the shippingService returns BASIC for the current order, transition to the enterBasicShippingDetails state. If the return value is EXPRESS transition to the enterExpressShippingDetails state. If the return value is NONE transition to the placeOrder state."

2.6.2.11. ActionState XML - evaluate action

The following example constructs an ActionState from XML that executes a action that evaluates an expression against the flow request context and exposes the evaluation result:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">


        <action-state id="getNextInterviewQuestion">
            <evaluate-action expression="flowScope.interview.nextQuestion()"/>
                <evaluation-result name="question"/>
            </evaluate-action>
            <transition on="success" to="displayQuestion"/>
        </action-state>

    </flow>
    			

This state definition reads "evaluate the flowScope.interview.nextQuestion() expression and expose the result under name question in the default scope."

The expression can evaluate any object traversable from the flow's org.springframework.webflow.execution.RequestContext. This example expression evaluates the nextQuestion method on the interview business object in flow scope.

2.6.2.12. ActionState XML - set action

The next example constructs an ActionState from XML that executes an action on a success transition that sets an attribute in "flash scope":

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <view-state id="selectFile" view="fileUploadForm">
            <transition on="submit" to="uploadFile"/>
        </view-state>

        <action-state id="uploadFile">
            <action bean="uploadAction" method="uploadFile"/>
            <transition on="success" to="selectFile">
                <set attribute="fileUploaded" scope="flash" value="true"/>
            </transition>
        </action-state>

    </flow>
    			

This flow definition reads "display the fileUploadForm. On form submit invoke the uploadFile method on the uploadAction. On success allow the user to select another file to upload. Report that the last file was uploaded successfully by setting the fileUploaded attribute in flash scope to true.

[Note]Note

Flash scoped attributes are preserved until the next user event is signaled into the flow execution. In this example this means the fileUploaded attribute is preserved across a redirect to the selectFile view state and any subsequent browser refreshes. Only when the submit event is signaled will the flash scope be cleared.

2.6.2.13. When to use which kind of action?

Simple action, Multi action, bean action, evaluate action, set? When to use one or the other?

Table 2.12.  Action implementation usage guidelines

Action typeUsage scenario
Simple (extends AbstractAction) You have a specialized behavior that stands on its own; for creating lightweight stubs or mocks for testing purposes.
MultiAction To group related command logic together. Particularly useful for when there are multiple related behaviors called by a flow.
Bean action When the logical behavior maps well to a method call on a service layer bean. When there is no "special" or exotic glue code required.
EvaluateAction When you need to invoke a bean in flow scope or evaluate any other flow expression.
SetAction When you need to set an attribute in flow or other scope during the course of flow execution.

2.6.3. DecisionState

When entered, a decision state makes a flow routing decision. This process consists of:

  1. Evaluating one or more boolean expressions against the executing flow to decide what state to transition to next.

The properties of a org.springframework.webflow.engine.DecisionState are summarized below:

Table 2.13. DecisionState properties

Property nameDescriptionCardinalityDefault value
transitions (inherited from TransitionableState) The transitions that are evaluated on an event occurrence that forms the basis for the decision. 1..* 

2.6.3.1. DecisionState XML - expression evaluation

The following example constructs a DecisionState from XML that evalutes a boolean expression to determine what transition to execute:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">
        ...

        <decision-state id="shippingRequired">
            <if test="${flowScope.order.needsShipping}" then="enterShippingDetails" else="placeOrder"/>
        </decision-state>

        ...

    </flow>
    			

This state definition reads "if the needsShipping property on the order object in flow scope is true, transition to the enterShippingDetails state, otherwise transition to the placeOrder state."

[Note]Note
Caution: flow definitions should not be vehicles for business logic. In this case the decision made was controller logic, reasoning on a pre-calculated value to decide what step of the flow to transition to next. That is the kind of logic that should be in a flow definition. In contrast, having the state itself embed the business rule defining how shipping status is calculated is a misuse. Instead, push such a calculation into application code where it belongs and instruct the flow to invoke that code using an action.

2.6.4. SubflowState

When entered, a subflow state spawns another flow as a subflow.

Recall that a flow is a reusable, self-contained controller module. The ability for one flow to call another flow gives you the ability to compose independent modules together to create complex controller workflows. Any flow can be used as subflow by any other flow, and there is a well-defined contract in play. Specifically:

  1. A Flow is an instance of org.springframework.webflow.engine.Flow.

  2. A newly launched flow can be passed input attributes which it may choose to map into its own local scope.

  3. An ending flow can return output attributes. If the ended flow was launched as a subflow, the resuming parent flow may choose to map these output attributes into its own scope.

It is helpful to think of the process of calling a flow like calling a Java method. Flows can be passed input arguments and can produce return values just like methods can. Flows are more powerful because they are potentially long-running, as they can span more than one request into the server.

The properties of a org.springframework.webflow.engine.SubflowState are summarized below:

Table 2.14. SubflowState properties

Property nameDescriptionCardinalityDefault value
subflow The definition of the flow to be spawned as a subflow. 1 
attributeMapper The strategy responsible for mapping input attributes to the subflow and mapping output attributes from the subflow. 0..*Null

When a SubflowState is entered, the following behavior occurs:

  1. The state first messages its attributeMapper, an instance of org.springframework.webflow.engine.FlowAttributeMapper, to prepare a Map of input attributes to pass to the subflow.

  2. The subflow is spawned, passing the input attributes. When this happens, the parent flow suspends itself in the subflow state until the subflow ends.

  3. When the subflow ends, a result event is returned describing the flow outcome that occurred. The parent flow resumes back in the subflow state.

  4. The resumed subflow state messages its attributeMapper to map any output attributes returned by the subflow into flow scope, if necessary.

  5. Finally, the resumed subflow state responds to the result event returned by the ended subflow by matching and executing a state transition.

The constructs used in spawning a flow as a subflow are shown graphically below:

SubflowState class diagram

2.6.4.1. SubflowState XML - with input attribute

The following example constructs an SubflowState from XML that spawns a shipping subflow:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <subflow-state id="enterShippingDetails" flow="shipping">
            <attribute-mapper>
                <input-mapper>
                    <mapping source="flowScope.order.shipping" target="shipping"/>
                </input-mapper>
            </attribute-mapper>
            <transition on="finish" to="placeOrder"/>
        </subflow-state>

        ...

    </flow>
    			

This subflow state definition reads "spawn the shipping flow and pass it the value of the shipping property on the order object in flow scope as an input attribute with the name shipping. When the shipping flow ends, respond to the finish result event by transitioning to the placeOrder state."

[Note]Note

The inner structure and behavior of the shipping flow is fully encapsulated within its own flow definition. A flow calling another flow as a subflow can pass that flow input and capture its output, but it cannot see inside it. Flows are black boxes. Because any flow can be used as a subflow, it can be reused in other contexts without change.

2.6.4.2. SubflowState API - input attributes

The following illustrates the equivalent example using the FlowBuilder API:

    public class OrderFlowBuilder extends AbstractFlowBuilder {
        public void buildStates() {
    	    ...
            addSubflowState("enterShippingDetails", flow("shipping"), shippingMapper(),
                transition(on("finish"), to("placeOrder")));
            ...
        }

        protected FlowAttributeMapper shippingMapper() {
            DefaultFlowAttributeMapper mapper = new DefaultFlowAttributeMapper();
            mapper.addInputMapping(mapping().source("flowScope.order.shipping").target("shipping").value());
            return mapper;
        }
    }
    			

2.6.4.3. Flow input mapping - input contract

Internally within the definition of the shipping flow referenced above, the flow may choose to map the shipping input attribute into its own scope using its input mapper when it starts. Any input attributes must be explictly mapped, defining the input contract for the flow:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        <input-mapper>
            <input-attribute name="shipping"/>
        </input-mapper>

        ...

    </flow>
    			

This short-form input mapper declaration reads "when a new execution of this flow starts map the shipping input attribute into flowScope under the name shipping."

[Note]Note

Had this input mapping not been defined the shipping attribute made available as input to this flow by a calling parent flow or external client would have been ignored.

2.6.5. EndState

When entered, an end state terminates a flow. A EndState represents exactly one logical flow outcome; for example, "finish", or "cancel".

If the ended flow was acting as a top-level or root flow the entire flow execution ends and cannot be resumed. In this case the end state is responsible for making a ViewSelection that is the basis for the ending response (for example, a confirmation page, or a redirect request to another flow or an external URL).

If the ended flow was acting as a subflow, the spawned subflow session ends and the calling parent flow resumes by responding to the end result returned. In this case the responsibility for any ViewSelection falls on the parent flow.

Once a flow ends any attributes in flow scope go out of scope immediately and become eligible for garbage collection.

As outlined, an end state entered as part of a root flow messages its ViewSelector to make a ending view selection. Typically this is a redirect-based ViewSelector, allowing for redirect after flow completion. An end state entered as part of a subflow is not responsible for a view selection; this responsibility falls on the calling flow.

2.6.5.1. EndState result events

When a EndState is entered it terminates a flow and, if used as subflow, returns a result event the parent flow uses to drive a state transition from the calling subflow state. It is the end state's responsibility to create this result event which is the basis for communicating the logical flow outcome to callers.

By default, an EndState creates a result event with an identifier that matches the identifier of the end-state itself. For example, an end state with id finish returns a result event with id finish. Also, any attributes in flow scope that have been explicitly mapped as output attributes are returned as result event parameters. This allows you to return data along with the logical flow outcome.

Spring Web Flow gives you full control over the ending view selection strategy, as well as what flow attributes should be exposed as output on a per EndState basis. These configurable properties are summarized below:

2.6.5.2. EndState Properties

Table 2.15. EndState properties

Property nameDescriptionCardinalityDefault value
viewSelectorThe strategy that makes the ending view selection when this state is entered and the flow is a root flow.0..1Null
outputMapper The service responsible for exposing flow output attributes, making those attributes eligible for output mapping by a calling flow. 0..1None

2.6.5.3. EndState XML - redirect to flow after completion

The following example constructs an EndState from XML that terminates a shipping subflow and requests a redirect response to another flow:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <end-state id="finish" view="flowRedirect:searchFlow"/>

    </flow>
    			

This end state definition reads "terminate the order flow and redirect to a new execution of the searchFlow".

2.6.5.4. EndState XML - redirect after flow completion

The following example constructs an EndState from XML that terminates a shipping subflow and requests a redirect response to an external URL:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <end-state id="finish" view="externalRedirect:/orders/${flowScope.order.id}"/>

    </flow>
    			

This end state definition reads "terminate the order flow and redirect to the URL returned by evaluating the /orders/${flowScope.order.id} expression."

This is an example of the familiar redirect after post pattern where after transaction completion a redirect is issued allowing the result of the transaction to be viewed (in this case using REST-style URLs).

2.6.5.5. EndState XML - flow output attribute

The following example constructs an EndState from XML that terminates a shipping subflow:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <end-state id="finish">
            <output-mapper>
                <output-attribute name="shipping"/>
            </output-mapper>
        </end-state>

    </flow>
    			

This end state definition reads "terminate the shipping flow and expose the shipping property in flow scope as an output attribute with name shipping."

2.6.5.6. EndState API - flow output attribute

The following illustrates the equivalent example using the FlowBuilder API:

    public class ShippingFlowBuilder extends AbstractFlowBuilder {
    	public void buildStates() {
    	    ...
            addEndState("finish",
                new DefaultAttributeMapper().add(
                   mapping().source("flowScope.shipping").target("shipping").value()
                );
        }
    }
    			

Since this end-state does not make a view selection it is expected this flow will be always used as a subflow. When this flow ends, the calling parent flow is expected to respond to the finish result, and may choose to map the shipping output attribute into its own scope.

2.6.5.7. SubflowState XML - mapping an output attribute

The next example shows how a subflow-state can respond to the ending result of a subflow and map output attributes into its own scope:

    <?xml version="1.0" encoding="UTF-8"?>
    <flow xmlns="http://www.springframework.org/schema/webflow"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
              http://www.springframework.org/schema/webflow
              http://www.springframework.org/schema/webflow/spring-webflow-1.0.xsd">

        ...

        <subflow-state id="enterShippingDetails" flow="shipping">
            <attribute-mapper>
                <output-mapper>
                    <output-attribute name="shipping"/>
                </output-mapper>
            </attribute-mapper>
            <transition on="finish" to="placeOrder"/>
        </subflow-state>

        ...

    </flow>
    			

This subflow state definition reads "spawn the shipping flow as a subflow. When the shipping flow ends map the shipping output attribute into flow scope under the name shipping, then respond to the finish result event by transitioning to the placeOrder state."

[Note]Note

Had this output mapping not been defined the shipping attribute made available as output to this flow by the ending subflow would have been ignored.