Over a million developers have joined DZone.

Spring Webflow: An Example

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}