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:spring-doc.cn

FlowOutcome flowId(Map<String, Object> inputAttributes);

Where a FlowOutcome has the following signature:spring-doc.cn

public interface FlowOutcome {
   public String getName();
   public Map<String, Object> getOutputAttributes();
}

8.1. input

The input element declares a flow input attribute, as follows:spring-doc.cn

<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.spring-doc.cn

8.1.1. Declaring an Input Type

The type attribute declares the input attribute’s type:spring-doc.cn

<input name="hotelId" type="long" />

If an input value does not match the declared type, a type conversion is attempted.spring-doc.cn

8.1.2. Assigning an Input Value

The value attribute specifies an expression to which to assign the input value, as follows:spring-doc.cn

<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.spring-doc.cn

8.1.3. Marking an input as required

The required attribute enforces that the input is not null or empty, as follows:spring-doc.cn

<input name="hotelId" type="long" value="flowScope.hotelId" required="true" />

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:spring-doc.cn

<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.spring-doc.cn

8.2.1. Specifying the Source of an output Value

The value attribute denotes a specific output value expression, as follows:spring-doc.cn

<output name="confirmationNumber" value="booking.confirmationNumber" />

8.3. Checkpoint: Input/Output Mapping

You should review the sample booking flow with input/output mapping:spring-doc.cn

<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.spring-doc.cn