8. 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:
FlowOutcome flowId(Map<String, Object> inputAttributes);
Where a FlowOutcome
has the following signature:
public interface FlowOutcome {
public String getName();
public Map<String, Object> getOutputAttributes();
}
8.1. input
The input
element declares a flow input attribute, as follows:
<input name="hotelId" />
Input values are saved in flow scope under the name of the attribute.
For example, the input in the preceding example is saved under a name of hotelId
.
8.1.1. Declaring an Input Type
The type
attribute declares the input attribute’s type:
<input name="hotelId" type="long" />
If an input value does not match the declared type, a type conversion is attempted.
8.1.2. Assigning an Input Value
The value
attribute specifies an expression to which to assign the input value, as follows:
<input name="hotelId" value="flowScope.myParameterObject.hotelId" />
If the expression’s value type can be determined, that metadata is used for type coercion if no type
attribute is specified.
8.2. The output
Element
The output
element declares a flow output attribute.
Output attributes are declared within end-states that represent specific flow outcomes.
The following listing defines an output
element:
<end-state id="bookingConfirmed">
<output name="bookingId" />
</end-state>
Output values are obtained from flow scope under the name of the attribute.
For example, the output in the preceding example would be assigned the value of the bookingId
variable.
8.3. Checkpoint: Input/Output Mapping
You should review the sample booking flow with input/output mapping:
<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
https://www.springframework.org/schema/webflow/spring-webflow.xsd">
<input name="hotelId" />
<on-start>
<evaluate expression="bookingService.createBooking(hotelId, currentUser.name)"
result="flowScope.booking" />
</on-start>
<view-state id="enterBookingDetails">
<transition on="submit" to="reviewBooking" />
</view-state>
<view-state id="reviewBooking">
<transition on="confirm" to="bookingConfirmed" />
<transition on="revise" to="enterBookingDetails" />
<transition on="cancel" to="bookingCancelled" />
</view-state>
<end-state id="bookingConfirmed" >
<output name="bookingId" value="booking.id"/>
</end-state>
<end-state id="bookingCancelled" />
</flow>
The flow now accepts a hotelId
input attribute and returns a bookingId
output attribute when a new booking is confirmed.