2. Creating the Controller using Spring Web Flow

PROBLEM

An alternative to Spring MVC is Spring Web Flow, a project from Spring for simplifying the development of web applications. While Spring Web Flow is compatible with Spring MVC, Spring Web Flow uses flows instead of controllers for implementing the web layer of an application. Since Spring Web Flow has functionality for managing application state, it is best suited for implementing the functionality where the activity being performed by the end-user spans multiple page requests.

For example, an online commerce application typically has checkout functionality which spans multiple pages. One page may require that you confirm the items in your order, with the option to add/remove items and change quantities. The next page may solicit shipping information, including picking shipping options and providing a shipping address. The next page is used for entering billing info. The bottom line is that the checkout process is a unit of work, and there are many intermediate steps to complete the checkout process.

With Spring Web Flow the group of related steps to accomplish a task are referred to as a Flow. While this functionality can be implemented with Spring MVC, Spring Web Flow has features that make it considerably easier. However in order to gain the benefits of Spring Web Flow, a developer needs to have a pretty in-depth knowledge of Spring and Spring Web Flow, and Spring Web Flow adds additional configuration requirements to your web application. While authoring the flow of an application using XML is elegant, implementing flows using XML can be difficult and tedious because it’s hard to visualize the application flow from the xml.

SOLUTION

Skyway Builder reduces the complexity of adding Spring Web Flow support by automatically bootstrapping the project for you. The moment you add the first flow to your project, Skyway Builder will add the necessary Spring configuration and libraries. That by itself is a significant time saver, but Skyway Builder goes one step further in regards to Spring Web Flow

In addition to configuring your application, Skyway builder also provides a custom Spring Web Flow editor for authoring flows. The flow editor is very similar to Skyway Builder’s action editor, except that you implement flows instead of operations.

You start with an empty canvas which represents a single flow, and you drop different web flow states (i.e. view, decision, action, subflow and stop) onto the canvas. State transitions are defined by drawing lines between the states and specifying the event associated with the transition. Actions can be added to flows and states.

HOW IT WORKS

Flows are a group of related steps for accomplishing a task in an application. Some of the benefits of using Spring Web Flow are

  1. XML-based flow definitions - The flow of the application is defined in XML. No custom Java is needed to implement a flow

  2. Expression language support - An expression language let’s you leverage logic from the other layers of the web application. Logic that a developer would typically put into a controller, that isn’t handled by Spring Web Flow, can beexpressed in XML using the expression language

  3. State management - Variables can be scoped (many scopes are supported), and Spring Web Flow will automatically handle the cleanup of those variables when they are out-of-scope.

  4. Modularization of flow logic - Flows can be re-used from other flows.

A Flow is responsible for orchestrating the task (steps) and supporting the user events associated with the task. Instead of the MVC-based approach of creating a controller and multiple operations for implementing a conversation, with Spring Web Flow you define a flow with states and expressions. Instead of the MVC-based approach of defining URL mappings, with Spring Web Flow you define events. Spring Web Flow also let's you easily separate your logic for handling a user event (on event id) from the pre-load logic (on entry) for a particular state.

Figure 2.3. Controller (MVC)

Controller (MVC)

Steps for creating a Flow:

  1. Right click the on a Model Package, and select New-->Flow

  2. From the New Web Flow Wizard, enter a common name for the flow. The common name is for referencing the controller from other artifacts. Click Next

  3. From the Create Webflow Domain Model panel, specify the name and location of the domain model. As you make changes to the web flow diagram, the flow domain model will be updated with Spring Web Flow specific content. The domain model is what's deployed with your application and interpretted by the Spring Web Flow framework. The domain model must be located in a folder withing WEB-INF. Click Finish.

  4. Implement the flow using the flow diagram,

The following figures shows a sample flow diagram.

Figure 2.4. Spring Web Flow - Sample Diagram

Spring Web Flow - Sample Diagram

RELATED RECIPES

  1. Creating the Controller using Spring Web Flow

  2. Creating the Model for Spring Web Flow

  3. Creating the View for Spring MVC

copyright@2023. skywayperspectives. All rights reserved