About this book

Many web applications need to take the user through a defined series of steps such as e-commerce checkouts or user registrations. Spring Web Flow works well for rich and flexible user interaction, additionally it helps you to describe the flow of websites in complex processes. Spring Web Flow 2 provides the perfect way to build these kinds of features, keeping them secure, reliable, and easy to maintain.

This book provides a platform on which you can build your own applications and services. It gives detailed information on Spring basics and covers core topics involving testing, security, and so on. We develop a complete, robust web application using the latest version of Spring, where page navigation is done on-the-fly.

This book teaches you how to work with Spring Web Flow. It covers both basic and advanced aspects and provides a detailed reference of the features Spring Web Flow. The book helps readers to extend the framework.

The integration of Spring and Java Server Pages is clearly explained in the book. The book also explains the essential modules of the complete Spring framework stack and teaches how to manage the control flow of a Spring web application.

The Spring Faces module will provide integration between Spring Web Flow and Java Server Faces (JSF). Testing, an important aspect of the software development process is covered towards the end; the question of how to test a Spring Web Flow application is answered.

Publication date:
March 2009
Publisher
Packt
Pages
272
ISBN
9781847195425

 

Chapter 1. Introduction

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.

Three cornerstones: Spring, Spring MVC, and Spring Web Flow

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:

  • Spring Framework

  • Spring MVC

  • Spring Web Flow

We will visualize the three cornerstones in the following figure:

Spring Framework

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.

Spring MVC

Spring Model—View—Controller (MVC) is the base for web framework from SpringSource. It provides a complete implementation of the widely known Model—View—Controller pattern.

Spring Web Flow

Spring Web Flow is the framework that is covered in this book. It is possible to use Spring Web Flow without Spring MVC, but the integration with this framework is seamless.

 

Three cornerstones: Spring, Spring MVC, and Spring Web Flow


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:

  • Spring Framework

  • Spring MVC

  • Spring Web Flow

We will visualize the three cornerstones in the following figure:

Spring Framework

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.

Spring MVC

Spring Model—View—Controller (MVC) is the base for web framework from SpringSource. It provides a complete implementation of the widely known Model—View—Controller pattern.

Spring Web Flow

Spring Web Flow is the framework that is covered in this book. It is possible to use Spring Web Flow without Spring MVC, but the integration with this framework is seamless.

 

What is Spring Web Flow


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.

The elements of Spring Web Flow: flow, view, and conversation

Around the Spring Web Flow Framework there are three important words, which have to be defined:

  • Flow

  • View

  • Conversation

Flow

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.

View

A view in Spring Web Flow is a single page that visualizes information.

Conversation

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:

The Spring Web Flow elements: an example

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.

 

The new major release: Spring Web Flow 2.0


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:

  • A domain-specific language for defining re-usable controller modules called flows

  • An advanced controller engine for managing conversational states

  • First-class support for using Ajax to construct rich user interfaces

  • First-class support for using JavaServer Faces with Spring

If you download the Spring Web Flow 2.0 distribution (http://www.springframework.org/download), you will get the following four modules:

  • Spring Web Flow

  • Spring Faces

  • Spring JavaScript

  • Spring Binding

The following figure visualizes the structure for the 2.0.3 distribution of Spring Web Flow:

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

Spring Faces is the module that connects Spring Web Flow with the JavaServer Faces (JSF) technology.

Spring JavaScript

Spring JavaScript is an encapsulation to add functionality on the client side to HyperText Markup Language (HTML) elements, for example, to add Asynchronous JavaScript and Extensible Markup Language (AJAX) features to the elements.

Spring Binding

Spring Binding is a library that helps you to bind data to the elements. This library is used internally by Spring Web Flow.

 

Introduction to a new version


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.

Automatic model binding

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.

Support for a new expression language

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.

Flash scope is now a real flash scope

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.

Spring Faces

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.

Flow managed persistence

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.)

External redirects

The external redirects inside Spring Web Flow 1 were always relative to the context. Now in Spring Web Flow 2, if the redirect begins with a slash, it is considered to be servlet relative, rather than context relative. URLs without a leading slash are still considered context relative.

 

Summary


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.

About the Authors

  • Markus Stäuble

    Markus Stäuble is currently working as CTO at namics (deutschland) gmbh. He has a Master degree in Computer Science. He started programming with Java in the year 1999. After that he has earned much experience in building enterprise java systems, especially web applications. He has a deep knowledge of the java platform and the tools and frameworks around Java.

    Browse publications by this author
  • Sven Lüppken

    Sven Lüppken has a degree in computer science, which he passed with distinction. He is currently employed as Java Software Developer at one of the leading broadcasting and production companies in Germany. Sven started programming in C and C++ at the age of 16 and quickly fell in love with the Java programming language during his studies.

    When he got the chance to write his diploma thesis about object-relational mapping technologies he accepted at once.

    Since then, he has integrated Hibernate and the JPA in many projects, always in conjunction with the Spring framework.

    Browse publications by this author
Book Title
Unlock this full book FREE 10 day trial
Start Free Trial