Thursday, December 10, 2009

Spring Web Flow 10000 ft Overview

Spring Web Flow (SWF) is a workflow engine designed specifically for Web page navigation. SWF flow definition language is a Domain-Specific Language (DSL) written in XML. Developers use this language to define State Machines.


Here is a visualized main flow from the swf-booking-faces sample project:


There are quite a few benefits of using workflow-based UI process frameworks such as Spring Web Flow. Among them are:
  1. UI flow are visible and easier to understand
  2. Complex UI interaction is abstracted and modularized into flows and can be reused

Other comparable technologies is Seam + jBMP.

When using Spring Web Flow, you basically replace "controllers" in MVC with flows, because flows capture controlling logic. Creating and setting models has become the responsibility of the flows.


The most commonly used state is ViewState. ViewState displays a view to user. User's action on that view, such as clicking a button, generates an event that is handled by the view state. Typically event handling results in transition to other state. Each view state has a corresponding view template. By default, view template is located at the same folder where the flow is located. For example: enterSearchCriteria view state has a view template enterSearchCriteria .xhtml.

<view-state id="enterSearchCriteria">
<on-render>
<evaluate expression="bookingService.findBookings(currentUser.name)" result="viewScope.bookings" result-type="dataModel" />
</on-render>
<transition on="search" to="reviewHotels">
<evaluate expression="searchCriteria.resetPage()" />
</transition>
<transition on="cancelBooking">
<evaluate expression="bookingService.cancelBooking(bookings.selectedRow)" />
<render fragments="bookingsFragment" />
</transition>
</view-state>


In this case, transition happens when user perform "search" action, which is defined in view template file as:

<sf:commandButton id="findHotels" value="Find Hotels" processIds="*" action="search" />

<on-render>is used to prepare the a ViewState for rendering. You can define actions in EL within a State to perform additional tasks such as invoking business service. Actions can be inserted into different places within a State: on state entry, on state exit, on view render, on transition etc.


Flow can have inputs (using <input>) and outputs (<output> elements in EndState).


Flow can also declare flow instance variables by using <var>.


Other type of states include:

  • ActionState: used to perform an action and transition to another state based on the outcome.
  • DecisionState: is similar to ActionState, with a different if-else syntax.
  • SubflowState: SubflowState invokes a subflow. Caller flow can pass in input parameters to subflow and check the output of the subflow to determine the transition.
  • EndState: flow is terminated when it enters in an EndState.


No comments:

Post a Comment