Spring Webflow: An Example
Spring Webflow: An Example
Join the DZone community and get the full member experience.Join For Free
Automist automates your software deliver experience. It's how modern teams deliver modern software.
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:webflow="http://www.springframework.org/schema/webflow-config" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"> <mvc:annotation-driven/> <!-- Webflow Configuration --> <webflow:flow-executor id="flowExecutor"/> <webflow:flow-registry id="flowRegistry"> <webflow:flow-location-pattern value="/WEB-INF/*-flow.xml"/> </webflow:flow-registry> <bean id="flowMappings" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> <property name="order" value="0"/> <property name="flowRegistry" ref="flowRegistry"/> </bean> <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> <property name="flowExecutor" ref="flowExecutor"/> </bean> </beans>
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd" <start-state="welcome"> <view-state id="welcome" view="/welcome.jsp"> <transition on="continue" to="finish"/> <transition on="cancel" to="cancelled"/> </view-state> <end-state id="cancelled" view="/cancelled.jsp"/> <end-state id="finish" view="/finish.jsp"/> </flow>
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.
Opinions expressed by DZone contributors are their own.