Nearly every day you read about a new release of a framework for developing web based applications. The Spring Framework is no exception. What makes Spring Web Flow(SWF) (second version, unless explicitly mentioned otherwise) special is that this framework not only solves one part of the tasks that have to be done in the development of a web application, but also helps you organize the flow (the order in which pages are called) inside your web application. Additionally, it manages the storage of data. However, to build a complete web application, you need more than Spring Web Flow. Therefore, we will also explain how to integrate Spring Web Flow with other frameworks.
Note
This book is neither a reference documentation, nor does it replace the reference documentation of the Spring Web Flow Framework. If you are interested in the reference documentation, we strongly recommend the reference from SpringSource. It is available online at the Uniform Resource Locator (URL) http://static.springframework.org/spring-webflow/docs/2.0.x/reference/html/index.html. If you need more information about the Spring Framework, visit the web page of the framework at the URL http://www.springframework.org.
Note
If you need more help with your daily development of frameworks from the Spring Portfolio, please visit http://www.springsource.org/. The site provides more information about all of the different frameworks. If the available reference documentation is insufficient for your needs, you can search the forums that are offered on that page. The start page for the forums is http://forum.springsource.org/.
Besides the theoretical basics of the Spring Web Flow Framework, we will show you many examples in the later chapters. For the examples, we have chosen a bug-tracking application because it is common to have a bug tracker inside a software project. We developed some model classes, which we will use in all our examples. All the classes are shown in the Appendix A1: flow.tracâThe Model for the Examples. You can use the classes under the Apache License Version 2.0. For more information about the license, please visit http://www.apache.org/licenses/LICENSE-2.0.html.
In this chapter, we will give a brief introduction to the essential modules of the complete Spring Framework stack: Spring, Spring MVC, and Spring Web Flow. Then we will explain the Spring Web Flow elements: flow, view, and conversation. This will be followed by the new features and modules of Spring Web Flow 2.0.
Before we start explaining and writing an application with the Spring Web Flow Framework, we want to give you a small overview of the essential modules of the complete Spring Framework stack. You should know these for writing an application based on the Spring Web Flow Framework. The three cornerstones are:
We will visualize the three cornerstones in the following figure:

The Spring Framework is the base for all other modules (frameworks) inside the Spring portfolio. It was initially developed as a dependency injection container (this principle is also known as inversion of control). Besides the dependency injection, the second cornerstone of the Spring Framework is aspect-oriented programming (AOP).
The current version of the Spring Framework is far more than that. It is the base for a complete stack for building enterprise Java applications.
Before we start explaining and writing an application with the Spring Web Flow Framework, we want to give you a small overview of the essential modules of the complete Spring Framework stack. You should know these for writing an application based on the Spring Web Flow Framework. The three cornerstones are:
We will visualize the three cornerstones in the following figure:

The Spring Framework is the base for all other modules (frameworks) inside the Spring portfolio. It was initially developed as a dependency injection container (this principle is also known as inversion of control). Besides the dependency injection, the second cornerstone of the Spring Framework is aspect-oriented programming (AOP).
The current version of the Spring Framework is far more than that. It is the base for a complete stack for building enterprise Java applications.
Whenever you read about Spring Web Flow on the Internet, you will find the expression conversationalâyou can use Spring Web Flow to create conversational web applications.
But what does that expression mean? It means that a user can interact with the application in a quite natural way. The application asks for information. After you have entered it, you can send it back to the application, which processes the data. In most cases, the application asks for more information.
For example, take an application with a wizard-like interface. Usually, wizard-like applications consist of multiple pages that are displayed one after the other. You can enter some information and then proceed to the next page, where you can enter additional information. If you think you've made mistakes, you can always go back to the previous page. Take a look at the data you have entered and correct them if they are wrong. Although you can go back any time you like, you can use the application only in the way the authors intended it to be used. You are working in a predefined flow with a specific goal, such as ordering a book or creating a new user account.
Although you can definitely write applications with this behavior using different technologies (even with pure Spring MVC), Spring Web Flow makes it very easy to create flows. Flows created by Spring Web Flow are not only decoupled from the applications logic, but are also re-usable in different applications. A flow in Spring Web Flow is a sequence of steps, that is, with states and transitions between them. There are also actions that can be executed on various points, for example, when the flow starts or a web page is rendered.
We have already mentioned that each flow exists for the purpose of reaching a defined goal. This means that you can use Spring Web Flow for all kinds of web applications with a predefined outcome such as user registration or login forms.
Note
Use the latest version of Spring Web Flow 2
We started writing this book with an early version of Spring Web Flow. While writing, some minor versions of Spring Web Flow were released (for example: 2.0.3, 2.0.4, and 2.0.5). It is highly recommended to use the latest available version of the framework. To see the fixes, you can look into the bug tracker for the Spring projects available at http://jira.springsource.org.
Around the Spring Web Flow Framework there are three important words, which have to be defined:
Flow
View
Conversation
A flow is a self-contained business process representing a real-world use case. Typically, a flow consists of some views, and the data is stored inside a conversation. From a more technical viewpoint, a flow encapsulates a re-usable sequence of steps that can be executed in different contexts.
First, the flow describes the order and the requirements when the views are shown. Additionally, actions can be executed. Between the executions of a flow, a conversation holds the data of the user.
In traditional web applications, you have the scopes: request, session
, and application
. Many use cases in a web application consist of more than one page. Therefore, you need more than the request
instance to store the data. A request
is often not enough, and a session
is too much. For this case, Spring Web Flow introduces the concept of a conversation.
For better understanding, we will visualize the conjunction of these three important concepts in the following figure:

Now that we have described the three cornerstonesâflow, view, and conversation, we want to explain the three elements and their relationship in a small example.
Imagine we have a portal where the user has to log in to see the content of the portal. We have the following three pages: login.xhtml, portal.xhtml
, and error.xhtml
. The login.xhtml
is the page that is shown if the user is not logged into the portal. If the user is successfully logged in, the portal.xhtml
page is shown. If the login fails, the error.xhtml
page is shown. The following figure shows an example page flow:

The flow describes both the circumstances: a successful login and an unsuccessful login. Moreover, the transitions between the pages are described. The single pages (login.xhtml
, portal.xhtml
, and error.xhtml
) are the views inside the flow. The conversation holds the data until a flow is executed. In the given example, the conversation stores the instance of an example class, User
.
In mid-2008, Version 2.0, the new major version of Spring Web Flow was released. The following list shows a few main features of the new version:
If you download the Spring Web Flow 2.0 distribution (http://www.springframework.org/download), you will get the following four modules:
The following figure visualizes the structure for the 2.0.3 distribution of Spring Web Flow:

Spring Web Flow is a framework in itself. It manages the handling of the flow with its conversation and views.
Spring Faces is the module that connects Spring Web Flow with the JavaServer Faces (JSF) technology.
To those readers who are familiar with the older version of Spring Web Flow, we want to give a small overview on what has really changed from Version 1.
The following concepts were added to the mentioned release of the Spring Web Flow Framework:
Automatic model binding
Support for a new expression language
Flash scope is now a real Flash scope
Spring Faces
Flow managed persistence
External redirects
A complete list of features is shown in Chapter 12 of the reference documentation of Spring Web Flow 2. This documentation is available online at http://static.springframework.org/spring-webflow/docs/2.0.x/reference/html/ch12.html.
In Version 1 of Spring Web Flow, you had to manually do the binding for your model classes. For this version, the class FormAction
(package org.springframework.webflow.action
) exists. The most notable methods are setupForm
and bindAndValidate
(see the following example).
<view-state id="display" view="sampleView"> <render-actions> <action bean="formAction" method="setupForm"/> </render-actions> <transition on="submit" to="show"> <action bean="formAction" method="bindAndValidate"/> </transition> </view-state>
The new release of Spring Web Flow now supports an automatic model
binding through the usage of the model
attribute inside the view-state
.
In Spring Web Flow 1, only Object-Graph Navigation Language (OGNL) is supported as an Expression Language (EL) within the flow definition files. Now support for the Unified EL is added. OGNL, of course, is still supported.
In Version 1 of Web Flow, the flash
scope lived across the current request and into the next request. This is similar to the view
scope inside the Version 2 of Spring Web Flow. In Web Flow 2, the flash
scope is cleared after every view render. Now the flash
scope is consistent with other web frameworks.
The integration of JavaServerFaces
is significantly improved with Ajax-enabled, custom JavaServer Faces (JSF) UICommand
components, and an event-driven, action-binding approach. The name of the module is Spring Faces.
Inside a web application, you have to deal with data: you have to read them from a database and store them to a database. Spring Web Flow offers the concept of flow managed persistence. With this feature, a flow can create, commit, and close an object persistence context for you. The framework integrates with both the object persistence technologies: Hibernate and Java Persistence API (JPA). (For more information on Hibernate Framework, visit http://www.hibernate.org; for more information on JPA read the Java Persistence FAQ from Sun available at http://java.sun.com/javaee/overview/faq/persistence.jsp.)
This chapter covers a brief overview of the new major release of Spring Web Flow. We mentioned the use of a bug-tracking application for the examples, which we will show in the following chapters.
In this chapter, we also explained the three frameworks: Spring, Spring MVC, and Spring Web Flow. The three cornerstones of Spring Web Flowâflow, view, and conversationâwere also explained using practical examples. For the example, we assumed a login to a portal. Last, but not the least, we showed you the new features that are offered by Spring Web Flow 2. In the following chapter, we will explain how to install Spring Web Flow 2 and show an example that you can run on your local machine. With this example, you will get an idea of how to build your own web application using Spring Web Flow 2.