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.spring-doc.cn

Within a flow, there are several points where you can execute actions:spring-doc.cn

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.spring-doc.cn

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:spring-doc.cn

<evaluate expression="entityManager.persist(booking)" />

7.1.1. Assigning an evaluate Result

If the expression returns a value, that value can be saved in the flow’s data model, called flowScope, as follows:spring-doc.cn

<evaluate expression="bookingService.findHotels(searchCriteria)" result="flowScope.hotels" />

7.1.2. Converting an evaluate Result

If the expression returns a value that may need to be converted, you can specify the desired type by using the result-type attribute, as follows:spring-doc.cn

<evaluate expression="bookingService.findHotels(searchCriteria)" result="flowScope.hotels"
          result-type="dataModel"/>

7.2. Checkpoint: Flow Actions

You should review the sample booking flow with actions added: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="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.spring-doc.cn