7.作
大多数流需要表达的不仅仅是视图导航逻辑。 通常,他们还需要调用应用程序的业务服务或其他作。
在流中,您可以在多个点执行作:
-
流开始时
-
在状态进入时
-
查看时渲染
-
在转换执行时
-
在 state exit 时
-
流结束时
作是使用简洁的表达式语言定义的。 默认情况下, Spring Web Flow 使用 Unified EL。 接下来的几节介绍了定义作的基本语言元素。
7.1. 使用evaluate
元素
最常用的 action 元素是evaluate
元素。
这evaluate
元素在流中的某个点计算表达式。
使用这个单个元素,您可以在 Spring bean 或任何其他流变量上调用方法。
下面的清单显示了一个示例:
<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
flow scope 中的对象。
要预订的酒店的 ID 是从 flow input 属性获取的。