Expert Spring MVC and Web Flow

(Dana P.) #1
Listing 12-27.Correctly Defined Decision State

<decision-state>
<if test="${flowScope.object.booleanProperty}" then="stateA" />
<if test="${this is called if the above test evaluates to false}" then="stateB"/>
<if test="${this is called if the above test evaluates to false}" then="stateC"➥
else ="stateD"/>
</decision-state>

■CautionIt is generally wise to put an elsebranch on the last if; otherwise, you run the risk of Spring
Web Flow not being able to find a matching transition.

Finally, a decision state can be used to invoke a single action that is used as the basis for
the decision. In this case, the action’s result event becomes the primary basis for the transition
decision.
When combined with a POJO action, this allows you to call a method in application code
that returns a single value that can be used as the basis for a routing decision. The decision
state will automatically adapt the method return value to an appropriate action result event
identifier according to a set of rules.
The rules are simple: for example, one rule is if a method return value is a java.lang.Boolean
a yesor noresult event will be returned. Table 12-12 details the exact behavior for adapting
method return values of different types.

Table 12-12.Behavior of POJO Return Values
Return Value Event Identifier
null null
true yes
false no
Enum The Stringrepresentation of the Enum

This allows you to implement branching logic very conveniently. Assuming our backing
object (purchase) has a method boolean isTaxable(TaxationRule rule), in our web flow defi-
nition we can write the code shown in Listing 12-28.

Listing 12-28.Listing of a POJO to Be Used As an Action

<decision-state id="determineTaxable">
<action bean="orderClerk" method="isTaxable(${flowScope.taxationRule})"/>
<transition on="yes" to="enterTaxDetails"/>
<transition on="no" to="lucky"/>
</action-state>

366 CHAPTER 12 ■ADVANCED SPRING WEB FLOW

Free download pdf