Defining FlowsIntroduction
This chapter begins the Users Section.
It shows how to implement flows using the flow definition language.
By the end of this chapter you should have a good understanding of language constructs, and be capable of authoring a flow definition.
What is a flow?
A flow encapsulates a reusable sequence of steps that can execute in different contexts.
Below is a Garrett Information Architecture diagram illustrating a reference to a flow that encapsulates the steps of a hotel booking process:
Site Map illustrating a reference to a flow
What is the makeup of a typical flow?
In Spring Web Flow, a flow consists of a series of steps called "states".
Entering a state typically results in a view being displayed to the user.
On that view, user events occur that are handled by the state.
These events can trigger transitions to other states which result in view navigations.
The example below shows the structure of the book hotel flow referenced in the previous diagram:
Flow diagram
How are flows authored?
Flows are authored by web application developers using a simple XML-based flow definition language.
The next steps of this guide will walk you through the elements of this language.
Essential language elementsflow
Every flow begins with the following root element:
]]>
All states of the flow are defined within this element.
The first state defined becomes the flow's starting point.
view-state
Use the view-state element to define a step of the flow that renders a view:
]]>
By convention, a view-state maps its id to a view template in the directory where the flow is located.
For example, the state above might render /WEB-INF/hotels/booking/enterBookingDetails.xhtml
if the flow itself was located in the /WEB-INF/hotels/booking directory.
transition
Use the transition element to handle events that occur within a state:
]]>
These transitions drive view navigations.
end-state
Use the end-state element to define a flow outcome:
]]>
When a flow transitions to a end-state it terminates and the outcome is returned.
Checkpoint: Essential language elements
With the three elements view-state, transition, and end-state, you can quickly express your view navigation logic.
Teams often do this before adding flow behaviors so they can focus on developing the user interface of the application with end users first.
Below is a sample flow that implements its view navigation logic using these elements:
]]>
Actions
Most flows need to express more than just view navigation logic.
Typically they also need to invoke business services of the application or other actions.
Within a flow, there are several points where you can execute actions. These points are:
On flow startOn state entryOn view renderOn transition executionOn state exitOn flow end
Actions are defined using a concise expression language. Spring Web Flow uses the Unified EL by default.
The next few sections will cover the essential language elements for defining actions.
evaluate
The action element you will use most often is the evaluate element.
Use the evaluate element to evaluate an expression at a point within your flow.
With this single tag you can invoke methods on Spring beans or any other flow variable.
For example:
]]>
Assigning an evaluate result
If the expression returns a value, that value can be saved in the flow's data model called flowScope:
]]>
Converting an evaluate result
If the expression returns a value that may need to be converted, specify the desired type using the result-type attribute:
]]>
Checkpoint: flow actions
Now review the sample booking flow with actions added:
]]>
This flow now creates a Booking object in flow scope when it starts.
The id of the hotel to book is obtained from a flow input attribute.
Input/Output Mapping
Each flow has a well-defined input/output contract.
Flows can be passed input attributes when they start, and can return output attributes when they end.
In this respect, calling a flow is conceptually similar to calling a method with the following signature:
inputAttributes);]]>
... where a FlowOutcome has the following signature:
getOutputAttributes();
}]]>
input
Use the input element to declare a flow input attribute:
]]>
Input values are saved in flow scope under the name of the attribute.
For example, the input above would be saved under the name hotelId.
Declaring an input type
Use the type attribute to declare the input attribute's type:
]]>
If an input value does not match the declared type, a type conversion will be attempted.
Assigning an input value
Use the value attribute to specify an expression to assign the input value to:
]]>
If the expression's value type can be determined, that metadata will be used for type coersion if no type attribute is specified.
Marking an input as required
Use the required attribute to enforce the input is not null or empty:
]]>
output
Use the output element to declare a flow output attribute.
Output attributes are declared within end-states that represent specific flow outcomes.
]]>
Output values are obtained from flow scope under the name of the attribute.
For example, the output above would be assigned the value of the bookingId variable.
Specifying the source of an output value
Use the value attribute to denote a specific output value expression:
]]>
Checkpoint: input/output mapping
Now review the sample booking flow with input/output mapping:
]]>
The flow now accepts a hotelId input attribute and returns a bookingId output attribute
when a new booking is confirmed.
Variables
A flow may declare one or more instance variables.
These variables are allocated when the flow starts.
Any @Autowired transient references the variable holds are also rewired when the flow resumes.
var
Use the var element to declare a flow variable:
]]>
Make sure your variable's class implements java.io.Serializable, as the instance state is saved between flow requests.
Calling subflows
A flow may call another flow as a subflow. The flow will wait until the subflow returns, then respond to the subflow outcome.
subflow-state
Use the subflow-state element to call another flow as a subflow:
]]>
The above example calls the createGuest flow, then waits for it to return.
When the flow returns with a guestCreated outcome, the new guest is added to the booking's guest list.
Passing a subflow input
Use the input element to pass input to the subflow:
]]>
Mapping subflow output
Simply refer to a subflow output attribute by its name within a outcome transition:
]]>
In the above example, guest is the name of an output attribute returned by the guestCreated outcome.
Checkpoint: calling subflows
Now review the sample booking flow calling a subflow:
]]>
The flow now calls a createGuest subflow to add a new guest to the guest list.