8. 输入/输出映射

每个流程都有明确的输入/输出约定。 流程在启动时可以传入输入属性,并在结束时返回输出属性。 从这个意义上来说,调用一个流程在概念上类似于调用具有以下签名的方法:spring-doc.cadn.net.cn

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

Where a FlowOutcome 具有以下签名:spring-doc.cadn.net.cn

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

8.1. 输入

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

<input name="hotelId" />

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

8.1.1. 声明输入类型

type 属性声明了输入属性的类型:spring-doc.cadn.net.cn

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

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

8.1.2. 分配输入值

value 属性指定要将输入值赋给的表达式,如下所示:spring-doc.cadn.net.cn

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

如果可以确定表达式的值类型,则在未指定type属性时,该元数据将用于类型转换。spring-doc.cadn.net.cn

8.1.3. 将输入标记为必填项

required 属性强制要求输入不能为空或空值,如下所示:spring-doc.cadn.net.cn

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

8.2. output元素

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

value 属性表示特定的输出值表达式,如下所示: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>

流程现在接受一个 hotelId 输入属性,并在新预订确认时返回一个 bookingId 输出属性。spring-doc.cadn.net.cn