7. 操作
大多数流程需要表达的不仅仅是视图导航逻辑。 通常,它们还需要调用应用程序的业务服务或其他操作。
在一个流程中,有多个点可以执行操作:
-
流程开始时
-
进入状态时
-
页面渲染时
-
执行转换
-
退出状态
-
流结束时
动作是通过使用简洁的表达式语言定义的。 默认情况下,Spring Web Flow 使用 Unified EL。 接下来的几个部分将介绍用于定义动作的基本语言元素。
7.1. evaluate元素
最常用的 action 元素是 evaluate 元素。 evaluate 元素会在您的流程中的某个点计算表达式。 通过这个单一的元素,您可以调用 Spring beans 或其他任何流程变量上的方法。 以下代码清单展示了一个示例:
<evaluate expression="entityManager.persist(booking)" />
7.2. 检查点:流操作
你应该查看添加了操作的示例预订流程:
<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" />
<end-state id="bookingCancelled" />
</flow>
此流程在启动时会在流程范围内创建一个 Booking 对象。 要预订的酒店 ID 是从流程输入属性中获取的。