Spring WebFlow Sneak Preview: Reuse and Framework Abstraction

Spring WebFlow Sneak Preview: Reuse and Framework Abstraction

he Spring framework, a lightweight open source J2EE container previously featured on these pages, is about to introduce a significant new feature as part of its already impressive offering. With the next release (1.3 , scheduled for summer of 2005) Spring will officially incorporate Spring WebFlow, an innovative, high-level Web framework for creating and configuring reusable Web flows within a Web application.

Those familiar with Spring may mistakenly assume Spring WebFlow is the same as Spring MVC. It’s not. Spring MVC is a lightweight framework for Web application development, while Spring WebFlow introduces a whole new value proposition. It enables Web application architects to focus on the more abstract concerns of Web application development: the structure of logical Web flows and the definition of Web event conditions. Furthermore, it introduces new concepts such as continuations (more on those shortly).

This article reviews some of the key concepts behind this upcoming framework, examining the major compositional elements of the flow markup language in the context of the sample login sequence flow, and explaining why WebFlow is an attractive framework. (Readers must have a basic knowledge of the Spring framework release 1.2.)

Spring WebFlow Concepts and Components

As a Web framework, WebFlow has advantages in both conceptual and practical terms. It is grounded in sound software engineering concepts that promote consistent and solid architectures, and its components are logical, easy to understand, and easy to work with?even on a large scale.

In fact, the framework is best suited for medium-sized to large applications development because its component model is optimized for architectural reuse and effective manageability and configurability.

Figure 1. WebFlow’s State Machine

Case Study in Context

As a case study, this article examines the most common Web application flow, a Web authentication sequence, and uses some of its segments for demonstration. A Web authentication sequence consists of a login form, a registration form, authentication action, registration action, and rendering the results for these actions. This example focuses mostly on the configuration for the components that render the login form.

Automata-Based Architecture

Spring WebFlow is based on the concept of the Finite State machine, also known as a Finite Automata. User interactions and transitions within a Web application are modeled on the common state machine principles of states and transitions?a very popular model in software engineering (see Figure 1). Automata-based design is an interesting design choice, and it is likely to gain popularity among application architects for its clarity and disciplined approach to modeling Web interactions.

Key Components of WebFlow

A Spring WebFlow application consists of one or more flows, which commonly include ActionStates that execute Actions, ViewStates, and Transitions.

A Flow

A flow is a predefined set of user interactions within a Web application expressed as a tree of action states and view states connected via transitions.

The following login-registration excerpt is an example of a Web flow with some typical components, such as loginForm, ActionState, and transitions. It is expressed in a WebFlow XML document that describes the composition of high-level flow components:

						...				...

Flow starts with the initial transition to the action state “loginForm”, and it ends with “successView”, which renders the view component for the main page “displayMain”. Flows can be captured either in an XML document or as Java classes that extend AbstractFlowBuilder.


ActionStates are reactions to user-generated or other events. ActionStates themselves can include executions of one or more executable Actions as well as transitions. In the following snippet, the “loginForm” ActionState consists of an executable action (“loginFormAction”) with only one possible transition defined (“success”) to “formView”:



Actions are atomic units of behavior that ActionStates can chain together, reuse, and declaratively invoke. Actions are extensions of the AbstractAction object, which implements the method doExecuteAction. This method accepts a RequestContext, a WebFlow object that encapsulates all the pertinent values passed from the flow. The loginForm ActionState invokes a LoginFormAction via Spring’s auto-wiring feature. Its doExecuteAction is automatically invoked, and it can perform some login setup operations, pre-population, etc.:

public LoginFormAction extends AbstractAction{ protected Event doExecuteAction(RequestContext context)  throws Exception{  //perform some login specific setup operations  ... }//end method}//end class


A ViewState initiates the rendering of some kind of a view component, such as jsp, tile, etc. In the following snippet, ViewState is invoked as a “success” transition from a loginForm ActionState. It will render the “enterCredentials” view component, which could be a Struts forward to a jsp or a tile:



As in a formal state machine, WebFlow defines states and transitions between states. WebFlow transitions are similar to Struts ActionForwards, and they are responsible for linking from one state to another.


Continuations allow backtracking and re-forking of program execution flows?a trip back in time from the program execution perspective. Continuations, originally a programming language concept, never formally made it into mainstream languages such as Java, VB, or C++. WebFlow implements continuations in a very innovative way. It uses them to allow for free navigation within a Web application, since WebFlow continuations can be used to persist and re-store any flow states.

Reuse with WebFlow

The flow definitions in WebFlow, like LoginSequence, are intentionally structured for reuse. They have well defined entry and exist states, and the states and transitions have well encapsulated lifecycles. The definitions are easy to configure and reuse with the Spring-enabled Web components. Architects should recognize and define common flows, such as login sequences, searches, and summary-details displays, and have them defined as reusable flows.

Furthermore, Actions, if architected properly in conjunction with the Spring framework, also are reusable components. They can be made parts of the action states in the multiple flows as well. This reuse, at both the overall Web-application and the flow levels in the action states, helps reduce the overall size of your application and improves its maintainability and understandability.

WebFlow Integration with Struts

WebFlow integrates with Struts out of the box, partly through the standard Spring classes and configurations available in the org.springframework.web.struts package. Integration with WebFlow-specific concepts, such as Actions and ActionFlows, requires only registering the Actions to be executed with WebFlow-specific Struts actions and incorporating them into an ActionFlow.

The following snippet is from struts-config.xml. It illustrates how the Struts Action is configured as an entry point to the flow. Notice the WebFlow-specific type for the Action class (org.springframework.web.flow.struts.FlowAction), and the property “flowId”, which connects the Action to the login sequence flow. The example login sequence is managed as a loginFlow bean in the Spring’s ApplicationContext. You would need to register the flow as a Spring-managed bean:


The example also uses the RequestProcessor from the Spring framework, (org.springframework.web.struts.BindingRequestProcessor) which enables Spring’s automatic form-to-bean object binding. This Spring feature prevents a proliferation of individual transient ActionForm objects:

As well, it requires the use of the convenience class, which auto-binds Web form values to Java objects’ properties:

The WebFlow pre-release, which is available for immediate download, contains a short and effective sample application (“birthdate”) that illustrates all the major points in this integration process. If you are interested in WebFlow, explore this sample application.

Spring MVC and Spring WebFlow as a Primary Framework

If you are building a major Web application from scratch, Spring’s Web offerings (Spring MVC and Spring WebFlow) are a very strong choice. Spring MVC is simple and intuitive, and as you would expect, it integrates perfectly with the rest of Spring’s managed components. On the other hand, the upcoming Spring WebFlow promises to deliver sophisticated and innovative features that will enable you to develop reusable and highly structured Web applications. These are very important qualities if you are developing large-scale enterprise applications. These same qualities have made the Struts framework popular.

Overall, J2EE Web frameworks are in a state of flux, having no clear technology leader. Struts is the most popular Web framework, but its chief architect and one of its lead developers have abandoned it. The proclaimed successor specification, JSF (Java Server Faces), has yet to win the hearts and minds of Struts developers. In the meantime, other frameworks are gaining respect and popularity.

You may be asking, “which way should I go?” With Spring WebFlow, you can defer that decision until you feel comfortable with one technology. WebFlow allows you to switch frameworks without any major impact on your Web applications. As previously noted, Spring WebFlow is an abstraction framework that lets you focus on logical flows within your Web application and on reuse. It makes frameworks such as Struts, JSF, and Spring MVC replaceable implementations of logical Web flows. This greatly eases the commitment of your technology choices.


Share the Post: