Spring Web Flow for better workflow management in JSF

An alternative to the JavaServer Faces CustomNavigationHandler

1 2 3 4 Page 3
Page 3 of 4

Handling new requirements

For a while the customer seems happy with the application. Then one day he invites you in for another meeting: "My Company is introducing two kinds of courses starting next month," he says. "There will be Web-based courses and classroom-based courses. The topics for each class may be the same or different. All Web-based courses are free for employees. Classroom-based courses are not. I want to incorporate these extra requirements into the existing application. Can you do it quickly?"

At the internal design meeting everyone agrees that this customer may come back in another two months with even more new requirements -- for instance, a new type of course called "in-house training." You will have to be prepared to adapt the application for these changing requirements. For this you will need a more configurable workflow management solution. Ideally, you want a rule-based solution that will let you easily handle new requirements.

Custom navigation in JSF

The next step is to create a solution that will allow you to configure rules in your existing JSF application. The luminaries behind JSF have given us a native method for incorporating custom navigation handling into JSF applications. Rather than relying on JSF's default NavigationHandler, you can write a CustomNavigationHandler. Figure 7 illustrates the process of incorporating custom navigation into an existing JSF application.

The process of incorporating custom navigation into a JSF application.
Figure 7. Incorporating custom navigation in JSF

As you can see, the process involves writing a CustomNavigationHandler and registering it in your faces-config.xml file. You can do whatever you like with this custom handler -- it even allows for navigation to non-JSF pages like those created using Struts 2 or a custom framework.

The thing is, you're on a short deadline now. Do you really want to write a custom workflow manager when you could leverage an existing open source framework famous for resolving just this sort of scenario?

Introducing Spring Web Flow

Spring Web Flow (SWF) allows you to represent a Web application's workflow in a clear and simple way. Spring Web Flow's reusable controller modules are completely configurable using XML, very much like JSF components but with more features. In Spring Web Flow, a Web flow (or flow) is composed of a set of states. A state is a point in the flow where something happens: for instance, showing a view or executing an action. Each state has one or more transitions that are used to move to another state. A transition is triggered by an event. In Spring Web Flow, states, transitions, and events collaborate to move the user across your UI from page to page.

In Spring Web Flow, adding customer requirements is reduced to writing a few lines of XML and then developing the new pages. If you design properly, you may avoid writing anything in your Java bean at all!

Consider the advantages of using flows rather than JSF's CustomNavigationHandler:

  • Flows can be designed to be self contained. This allows you to see a part of your application as a module and reuse it in multiple situations.
  • Flows can define any reasonable UI flow in a Web application, always using the same consistent technique. You're not forced into using specialized controllers for particular situations.

It seems that Spring Web Flow may be the ideal solution to satisfy your customer's new requirements. In fact, Figure 8 is a flow diagram illustrating how you could use Spring Web Flow to incorporate the new requirements into the Course Registration application.

The application's new requirements captured in a flow diagram.
Figure 8. New requirements captured in a flow diagram

The above diagram consists of a start state, where the Spring Web Flow transitions begin; a decision state, where the flow needs to take a decision based on a value; and an end state, denoting the end of transitions. Note that if the user selects a Web-based training, there is no need for a payment. If the selection is classroom-based the user is sent to the payment screen.

Spring Web Flow in your development environment

You'll need to set up your environment to use Spring Web Flow before continuing. You can start by creating a new directory under the Tomcat webapps directory, called Rave1. Spring Web Flow is a component of the Spring Framework's Web stack focused on the definition and execution of UI flow within a Web application. It has been architected as a self-contained flow engine with few required dependencies or third-party APIs. All dependencies are carefully managed.

At a minimum, to use Spring Web Flow you need to download and install the following from the Spring Web Flow homepage:

  • spring-webflow (the framework)
  • spring-core (miscellaneous utility classes used internally by the framework)
  • spring-binding (the Spring data binding framework, used internally)
  • commons-logging (a simple logging facade, used internally)
  • OGNL (the default expression language)
XhtmlHelper and Shale
XhtmlHelper is a helper bean for rendering links to download resources commonly used in HTML and XHTML pages. Shale is a Web application framework fundamentally based on JavaServer Faces. Spring includes Shale-related jars to allow seamless integration between different architectures and frameworks.

For my development environment (which you should reproduce) I have used the latest releases available at the time of writing this article, mainly Spring Framework 2.1 and Spring Web Flow 2.0. In my Rave1\WEB-INF\lib directory I have all of the above files except the commons-logging file. I already had the logging-related files in my Tomcat directory, so didn't need to recopy them here. To avoid compilation issues due to an Apache XhtmlHelper class, I also have some Shale-related files in my lib directory. Figure 9 is a snapshot of my Rave1\WEB-INF\lib directory.

Files required for working with Spring Web Flow.
Figure 9. Files required for working with Spring Web Flow
1 2 3 4 Page 3
Page 3 of 4