8. 输入/输出映射
每个流都有一个定义明确的 input/output 协定。 流可以在开始时传递输入属性,也可以在结束时返回输出属性。 在这方面,调用流在概念上类似于调用具有以下签名的方法:
FlowOutcome flowId(Map<String, Object> inputAttributes);
其中 aFlowOutcome
具有以下签名:
public interface FlowOutcome {
public String getName();
public Map<String, Object> getOutputAttributes();
}
8.1. 输入
这input
元素声明一个 flow input 属性,如下所示:
<input name="hotelId" />
输入值将保存在流范围中的属性名称下。
例如,前面示例中的输入以hotelId
.
8.1.1. 声明输入类型
这type
attribute 声明 input 属性的类型:
<input name="hotelId" type="long" />
如果 input 值与声明的类型不匹配,则尝试进行类型转换。
8.2. 使用output
元素
这output
元素声明 flow output 属性。
输出属性在表示特定流结果的终端状态中声明。
下面的清单定义了一个output
元素:
<end-state id="bookingConfirmed">
<output name="bookingId" />
</end-state>
输出值是从属性名称下的流范围获取的。
例如,前面示例中的输出将被分配bookingId
变量。
8.3. 检查点:输入/输出映射
您应该查看包含输入/输出映射的示例预订流程:
<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
input 属性并返回一个bookingId
output 属性。