Guide to Spring Web Flow – Spring Web Flow指南

最后修改: 2017年 5月 20日

1. Overview


Spring Web Flow builds on Spring MVC and allows implementing flows within a web application. It’s used for creating sequences of steps that guide users through a process or some business logic.

Spring Web Flow建立在Spring MVC之上,允许在Web应用中实现流程。它用于创建步骤序列,引导用户完成一个流程或一些业务逻辑。

In this quick tutorial, we’ll go through a simple example of a user activation flow. The user is presented with a page and clicks on the Activate button to proceed or on the Cancel button to cancel activation.


Not that the assumption here is that we have an already set-up Spring MVC web application.

不是说这里的假设是,我们有一个已经设置好的Spring MVC网络应用。

2. Setup


Let’s start by adding the Spring Web Flow dependency into the pom.xml:

让我们先把Spring Web Flow的依赖关系加入pom.xml


The latest version of Spring Web Flow can be found in the Central Maven Repository.

Spring Web Flow的最新版本可以在Central Maven Repository中找到。

3. Creating a Flow


Let’s now create a simple flow. As stated earlier, flow is a sequence of steps that guides a user through a process. Currently, this can only be done using XML-based config.


Each step in the flow is called a state.


For this simple example, we’ll be using a view-state. A view-state is a step in the flow that renders a matching view. The view-state refers to a page in the app (WEB-INF/view), with the id of the view-state being the name of the page to which it refers.


We will also be using a transition element. A transition element is used for handling events that occur within a particular state.


For this example flow, we’ll set up three view-states – the activation, success, and failure.


The process for this flow is pretty straightforward. The starting point is the activation view. If an activate event is triggered, it should transition to the success view. If the cancel event is triggered, it should transition to the failure view. The transition element handles the button click event that happens in the view-state:


<view-state id="activation">
    <transition on="activate" to="success"/>
    <transition on="cancel" to="failure"/>

<view-state id="success" />

<view-state id="failure" />

The initial activation page, which is referred to by the id activation and located in WEB-INF/view/activation.jsp, is a simple page that has two buttons, activate and cancel. Clicking the buttons with trigger our transitions to either send the user to the success view-state (WEB-INF/view/success.jsp) or the failure view-state (WEB-INF/view/failure.jsp):


    <h2>Click to activate account</h2>

    <form method="post" action="${flowExecutionUrl}">
        <input type="submit" name="_eventId_activate" value="activate" />
        <input type="submit" name="_eventId_cancel" value="cancel" />

We’re using the flowExecutionUrl to access the context-relative URI for the current flow execution view-state.


4. Configuring the Flow


Next, we will configure Spring Web Flow into our web environment. We will do this by setting up a Flow Registry and Flow Builder Service.

接下来,我们将把Spring Web Flow配置到我们的网络环境中。我们将通过设置Flow注册表和Flow Builder服务来实现这一目标。

The Flow Registry allows us to specify the location of our flows and also specify a Flow Builder Service if one is being used.


The Flow Builder Service helps us customize services and settings used to build flows.


One of the services we can customize is the view-factory-creator. The view-factory-creator allows us to customize the ViewFactoryCreator used by Spring Web Flow. Since we are using Spring MVC, we can configure Spring Web Flow to use the view resolver in our Spring MVC configurations.

我们可以定制的服务之一是view-factory-creator视图工厂创建者允许我们定制Spring Web Flow使用的视图工厂创建者。由于我们使用的是Spring MVC,我们可以配置Spring Web Flow,使其在Spring MVC配置中使用视图解析器。

Here is how we’ll configure Spring Web Flow for our example:

下面是我们如何为我们的例子配置Spring Web Flow。

public class WebFlowConfig extends AbstractFlowConfiguration {

    private WebMvcConfig webMvcConfig;

    public FlowDefinitionRegistry flowRegistry() {
        return getFlowDefinitionRegistryBuilder(flowBuilderServices())
          .addFlowLocation("/WEB-INF/flows/activation-flow.xml", "activationFlow")

    public FlowExecutor flowExecutor() {
        return getFlowExecutorBuilder(flowRegistry()).build();

    public FlowBuilderServices flowBuilderServices() {
        return getFlowBuilderServicesBuilder()

    public MvcViewFactoryCreator mvcViewFactoryCreator() {
        MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
        return factoryCreator;

We can also use XML for that configuration:


<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="activationFlowRegistry"/>

<flow:flow-builder-services id="flowBuilderServices"

<bean id="mvcViewFactoryCreator" 
    <property name="viewResolvers" ref="jspViewResolver"/>

<flow:flow-registry id="activationFlowRegistry" 
    <flow:flow-location id="activationFlow" path="/WEB-INF/flows/activation-flow.xml"/>

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="activationFlowExecutor"/>
<flow:flow-executor id="activationFlowExecutor" 

5. Navigating the Flows


To navigate through the flows, start up the web app and go to http://localhost:8080/{context-path}/activationFlow. To start up the app, deploy it on an application server, such as Tomcat or Jetty.

要浏览这些流程,请启动 Web 应用程序并转到http://localhost:8080/{context-path}/activationFlow。要启动应用程序,请将其部署在应用服务器上,例如TomcatJetty

This sends us to the initial page of the flow, which is the activation page specified in our flow configuration:


activate account 1


You can click on the activate button to go the success page:


activation successful 1Or the cancel button to go to the failure page:

activation successful 1或者点击cancel按钮,进入失败页面。

activation failed 1

6. Conclusion


In this article, we used a simple example as a guide on how to use Spring Web Flow.

在这篇文章中,我们用一个简单的例子来指导如何使用Spring Web Flow。

You can find the complete source code and all code snippets for this article over on GitHub.