8. 输入/输出映射

每个流都有一个定义明确的 input/output 协定。 流可以在开始时传递输入属性,也可以在结束时返回输出属性。 在这方面,调用流在概念上类似于调用具有以下签名的方法:spring-doc.cadn.net.cn

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

其中 aFlowOutcome具有以下签名:spring-doc.cadn.net.cn

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

8.1. 输入

input元素声明一个 flow input 属性,如下所示:spring-doc.cadn.net.cn

<input name="hotelId" />

输入值将保存在流范围中的属性名称下。 例如,前面示例中的输入以hotelId.spring-doc.cadn.net.cn

8.1.1. 声明输入类型

typeattribute 声明 input 属性的类型:spring-doc.cadn.net.cn

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

如果 input 值与声明的类型不匹配,则尝试进行类型转换。spring-doc.cadn.net.cn

8.1.2. 分配输入值

valueattribute 指定要为其分配 input 值的表达式,如下所示:spring-doc.cadn.net.cn

<input name="hotelId" value="flowScope.myParameterObject.hotelId" />

如果可以确定表达式的值类型,则该元数据用于类型强制转换,如果 notype属性。spring-doc.cadn.net.cn

8.1.3. 根据需要标记输入

requiredattribute 强制输入不为 null 或为空,如下所示:spring-doc.cadn.net.cn

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

8.2. 使用output元素

output元素声明 flow output 属性。 输出属性在表示特定流结果的终端状态中声明。 下面的清单定义了一个output元素:spring-doc.cadn.net.cn

<end-state id="bookingConfirmed">
    <output name="bookingId" />
</end-state>

输出值是从属性名称下的流范围获取的。 例如,前面示例中的输出将被分配bookingId变量。spring-doc.cadn.net.cn

8.2.1. 指定output

valueattribute 表示特定的输出值表达式,如下所示:spring-doc.cadn.net.cn

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

8.3. 检查点:输入/输出映射

您应该查看包含输入/输出映射的示例预订流程:spring-doc.cadn.net.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>

流现在接受hotelIdinput 属性并返回一个bookingIdoutput 属性。spring-doc.cadn.net.cn