Over a million developers have joined DZone.

Spring Webflow: An Example

DZone's Guide to

Spring Webflow: An Example

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

In this article I will give an introduction to Spring Weblow. Using Spring Webflow we can create a guided navigation in the application. Webflow consist of many steps called states. Entering a state typically results in a view being displayed to the user. On that view, user events occur that are handled by the state. These events can trigger transitions to other states which result in view navigations.

Configuring Spring Webflow

Please see below the Spring context file with Webflow configurations.

<beans xmlns="http://www.springframework.org/schema/beans" 
        <!--  Webflow Configuration -->
        <webflow:flow-executor id="flowExecutor"/>
        <webflow:flow-registry id="flowRegistry">
            <webflow:flow-location-pattern value="/WEB-INF/*-flow.xml"/>
        <bean id="flowMappings" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
            <property name="order" value="0"/>
            <property name="flowRegistry" ref="flowRegistry"/>
        <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
            <property name="flowExecutor" ref="flowExecutor"/>

Here for any incoming request, the Spring Webflow registry will be checked to see whether any guided flows are configured. That is why we make the order for Spring Webflow ‘0’.  The flow registry contains all the flows available in the application. If a guided flow is found then the flow executor is initiated.

Next we will see the Spring Webflow file. Each guided flow has a separate file in the application. Here we will see a simple Webflow example. In order to start the flow, you can click on a link which will invoke the correct flow file.

<a href="welcome-flow.do">Start welcome flow</a>

Spring Webflow file

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
        <view-state id="welcome" view="/welcome.jsp">
            <transition on="continue" to="finish"/>
            <transition on="cancel" to="cancelled"/>
        <end-state id="cancelled" view="/cancelled.jsp"/>
        <end-state id="finish" view="/finish.jsp"/>


In this flow file, we set the start state to a state called welcome. Then we defined the view state for the welcome state. In this welcome state we have two different actions. These actions are defined in transition states. Based on the input from the user, request will be navigated to either a ‘cancelled’ state or a ‘finish’ state. Then we defined both the cancelled and finish states. 

I hope this will help you to start with a simple Spring Webflow use case.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}