11. 调用子流

一个流可以调用另一个流作为子流。 流会等待,直到子流返回并响应子流结果。spring-doc.cn

11.1. 元素subflow-state

该元素将另一个流作为子流调用,如下所示:subflow-statespring-doc.cn

<subflow-state id="addGuest" subflow="createGuest">
    <transition on="guestCreated" to="reviewBooking">
        <evaluate expression="booking.guests.add(currentEvent.attributes.guest)" />
    </transition>
    <transition on="creationCancelled" to="reviewBooking" />
</subflow-state>

前面的示例调用流并等待它返回。 当流程返回结果时,新客人将被添加到预订的客人列表中。createGuestguestCreatedspring-doc.cn

11.1.1. 传递 Sub-flow Input

该元素将 input 传递给 subflow,如下所示:inputspring-doc.cn

<subflow-state id="addGuest" subflow="createGuest">
    <input name="booking" />
    <transition to="reviewBooking" />
</subflow-state>

11.1.2. 映射子流输出

当子流完成时,其最终状态 ID 将作为用于继续导航的事件返回给调用流。spring-doc.cn

子流还可以创建调用流可以在结果转换中引用的输出属性,如下所示:spring-doc.cn

<transition on="guestCreated" to="reviewBooking">
    <evaluate expression="booking.guests.add(currentEvent.attributes.guest)" />
</transition>

在前面的示例中,是结果返回的 output 属性的名称。guestguestCreatedspring-doc.cn

11.2. 检查点:调用子流

您应查看调用子流的示例预订流: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="addGuest" to="addGuest" />
        <transition on="confirm" to="bookingConfirmed" />
        <transition on="revise" to="enterBookingDetails" />
        <transition on="cancel" to="bookingCancelled" />
    </view-state>

    <subflow-state id="addGuest" subflow="createGuest">
        <transition on="guestCreated" to="reviewBooking">
            <evaluate expression="booking.guests.add(currentEvent.attributes.guest)" />
        </transition>
        <transition on="creationCancelled" to="reviewBooking" />
    </subflow-state>

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

    <end-state id="bookingCancelled" />

</flow>

该流现在调用子流以将新来宾添加到来宾列表。createGuestspring-doc.cn