7. Actions
Most flows need to express more than view navigation logic. Typically, they also need to invoke business services of the application or other actions.
Within a flow, there are several points where you can execute actions:
-
On flow start
-
On state entry
-
On view render
-
On transition execution
-
On state exit
-
On flow end
Actions are defined by using a concise expression language. By default, Spring Web Flow uses the Unified EL. The next few sections cover the essential language elements for defining actions.
7.1. The evaluate
Element
The most often used action element is the evaluate
element.
The evaluate
element evaluates an expression at a point within your flow.
With this single element, you can invoke methods on Spring beans or any other flow variable.
The following listing shows an example:
<evaluate expression="entityManager.persist(booking)" />
7.2. Checkpoint: Flow Actions
You should review the sample booking flow with actions added:
<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>
This flow now creates a Booking
object in flow scope when it starts.
The ID of the hotel to book is obtained from a flow input attribute.