Create your first Spring Web Flow based web application

1. Introduction

Spring Web Flow is an extension to Spring MVC, that helps in implementing the flows in a complex web application. Hopefully, you might have taken an overview of Spring Web Flow in the previous article.

Following diagram shows the flow execution of the Spring Web Flow based application that we are going to create.

Spring Web Flow


2. Implementation

To quickly start with, let’s first configure out the dependencies in pom.xml file –

pom.xml

Let’s now configure the web.xml file.

web.xml

Based on the dispatcher <servlet-name> configuration in the web.xml file above, we now need to create the corresponding dispatcher-servlet.xml.

Note that –

  • This file is our application’s web application context and contains spring-related configuration.
  • The name “dispatcher-servlet.xml” comes from <servlet-name> configured in web.xml suffixed with “-servlet.xml“.
  • Make sure to add namespace and schema location for MVC context.

dispatcher-servlet.xml

We now need to create and configure the web flow XML for our application, based on the configuration done in the dispatcher servlet. (Refer to the highlighted section above)

memberShipFlow.xml

To understand the above Spring Web Flow XML, click here.

Let’s start creating the view files now.

index.jsp

memberDetails.jsp

confirmDetails.jsp

welcome.jsp

exception.jsp

We will now move on to create the java files, starting with the bean class.

UserBean.java

Let’s now create the controller and the handler class.

CombatController.java

Demohandler.java

3. Running the application

Now, let’s run it over on our server by right-clicking your project in Eclipse.

And choose Run As > Run on Server 

The flow goes as –

Spring WebFlow ExecutionOn the index page click Become Member. Clicking Become Member link will trigger webflow and the page with mapping of first state will be displayed (memberDetails.jsp)

Spring WebFlow executionIf we click on Submit without filling any details, the basic Spring validation will come into action and display appropriate message as follows:

Spring WebFlow ExecutionNow after filling required details and clicking Submit we will be directed to next state of  WebFlow (confirmDetails.jsp) as configured in memberShipFlow.xml

 

Spring WebFlow ExecutionNow clicking Edit will take us to previous state  (memberDetails.jsp) as:

Spring WebFlow ExecutionHere we can edit the details and Submit again.

Instead of Edit, clicking Confirm Details on ConfirmDetails page will take us to next state (welcome.jsp) as:

Spring WebFlow ExecutionNote that the Home link is given as global transition, clicking it anytime will end the Web Flow and redirect to Home Page that is Become Member page (index.jsp).

4. Download the source code

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.