2.6. Actions

Most flows need to express more than just 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. These points are:

Actions are defined using a concise expression language. Spring Web Flow uses the Unified EL by default. The next few sections will cover the essential language elements for defining actions.

evaluate

The action element you will use most often is the evaluate element. Use the evaluate element to evaluate an expression at a point within your flow. With this single tag you can invoke methods on Spring beans or any other flow variable. For example:

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

Assigning an evaluate result

If the expression returns a value, that value can be saved in the flow's data model called flowScope:

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

Converting an evaluate result

If the expression returns a value that may need to be converted, specify the desired type using the result-type attribute:

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

Checkpoint: flow actions

Now 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
                          http://www.springframework.org/schema/webflow/spring-webflow-2.0.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.