Archive | Developer RSS feed for this section

Spring Web Flow – basic concept

How can you implement the page flow for airline booking system with Struts alone? Before we go deep on the new technology, lets try to tackle it the old way. First, take a look at the page flow below first.

To implement a multi-step page flow in Struts,  we can chain individual actions together through the various views. Action URLs to process different events like “back” or “submit” are hard-coded into each view. Some form of ad-hoc session storage is used to manage flow state. Redirect after post is used to prevent duplicate submissions, etc. Although this is a simple and functional approach, it has a major disadvantage: the overall page flow of the web application is not clear from looking at the action definitions in the struts-config.xml file. That is to say, the flow is not explicitly defined but implicitly hide in your struts-config.xml. Besides, flexibility also suffers since actions and views cannot be easily reused.

This is where Spring Web Flow comes in, allowing you to represent the page flow of a web application in finite state machine. 

In finite state machine, a web flow is composed of a set of states. A state is a point in the flow where something happens; for instance, displaying a view (ie. ViewState) or executing an action (ie. ActionState). Each state has one or more transitions (ie. event) that are used to move to another state. In Spring Web Flow, an ActionState executes an action when entered. That action returns the logical result of its execution, and that result is mapped to a state transition. When an ViewState is entered, it will cause the executing flow to pause, and returns control back to the client with instruction to render the configured view. Later, after some user think-time, the client signals an event describing what action the user took. That resumes the flow, and the event that occurred is mapped to a state transition, which takes the user to the next step in the flow.

The state machine model can be reused anywhere, including environments like Struts, Spring MVC, Tapestry, JSF, and even Portlets. Besides, The page flow in a web application is clearly visible by looking at the corresponding web flow definition (in an XML file or Java class). The page flow now is clearly and explicitly specified in web flow definition (either in XML file or Java class). And it is self-contained and reusable in multiple situations. On top of that, it has a clear, observable lifecycle that is managed for you automatically.

Q & A

Q1) “… since the executing flow is paused when a ViewState is entered, and control is returned to the browser, how is the same flow picked up and resumed on subsequent events?”  The answer is the client tracks the unique id of the executing flow, and provides it as input when the next event is signaled. This is typically done using a hidden form field. This is the concept of continuation.

<input type=”hidden” value=”<c:out value=”${flowExecution.id}”/>”>

Q2) “What about decision state and subflow state?”  Decision state decisions what transition to take with conditional logic embedded. For a flow that is independent from the main flow, you can model it as subflow. When a subflow state is entered, a child flow is spawned. The parent flow is suspended until the child flow ends. This lets you view your application as a set of self-contained modules – flows – that you can easily embed in multiple situations in a consistent manner. By the way, you can pass information to the subflow.

Q3) “What about the end states of the state machine?”  When an end state is entered, the active flow session terminates. Upon termination, all resources associated with the flow are cleaned up for you automatically. So, a new scope “flow” is introduced that spans multiple requests but shorter than a session.

Q4) “When should I use web flow?”  It should be noted that Spring Web Flow is not a one-size-fits-all solution. As you’ve seen, it’s a stateful system that automates the management of page flows that drive business processes. It should not be used when simpler, stateless solutions are more appropriate. For example, it should not be used where sites require free navigations, where the user is free to “click around” anywhere they please. Spring Web Flow is designed to power controlled navigations, where the user is guided through a process with a clear business goal and lifecycle.

Reference

TheServerSide Spring Web Flow Article

Spring Web Flow Architecture

Spring Web Flow – Practical Introduction

Official Spring Web Flow Site

Leave a comment Continue Reading →