Guide to Spring Web Flow – Spring Web Flow指南

最后修改: 2017年 5月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

2.设置

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

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

<dependency>
    <groupId>org.springframework.webflow</groupId>
    <artifactId>spring-webflow</artifactId>
    <version>2.5.0.RELEASE</version>
</dependency>

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

3.创建一个流程

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.

现在让我们来创建一个简单的流程。如前所述,流程是引导用户完成一个流程的步骤序列。目前,这只能通过基于XML的配置来完成。

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.

对于这个简单的例子,我们将使用一个view-stateview-state是流程中的一个步骤,它渲染了一个匹配的视图。view-state指的是应用程序中的一个页面(WEB-INF/view),view-state的id就是它所指的页面的名称。

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

我们还将使用一个transition元素。一个transition元素用于处理在一个特定状态中发生的事件。

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:

这个流程是非常直接的。起始点是激活视图。如果触发了激活事件,它应该过渡到成功视图。如果触发了cancel事件,它应该过渡到failure视图。transition元素处理发生在视图状态的按钮点击事件:

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

<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):

最初的激活页面,被称为activation,位于WEB-INF/view/activation.jsp,是一个简单的页面,有两个按钮:activatecancel。点击这些按钮会触发我们的转换,将用户送到成功的视图状态WEB-INF/view/success.jsp)或者失败的视图状态WEB-INF/view/failure.jsp):

<body>
    <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" />
    </form>
</body>

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

我们使用flowExecutionUrl来访问当前流程执行view-state的上下文相关的URI。

4. Configuring the Flow

4.配置流量

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。

@Configuration
public class WebFlowConfig extends AbstractFlowConfiguration {

    @Autowired
    private WebMvcConfig webMvcConfig;

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

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

    @Bean
    public FlowBuilderServices flowBuilderServices() {
        return getFlowBuilderServicesBuilder()
          .setViewFactoryCreator(mvcViewFactoryCreator())
          .setDevelopmentMode(true).build();
    }

    @Bean
    public MvcViewFactoryCreator mvcViewFactoryCreator() {
        MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
        factoryCreator.setViewResolvers(
          Collections.singletonList(this.webMvcConfig.viewResolver()));
        factoryCreator.setUseSpringBeanBinding(true);
        return factoryCreator;
    }
}

We can also use XML for that configuration:

我们也可以使用XML进行配置。

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

<flow:flow-builder-services id="flowBuilderServices"
  view-factory-creator="mvcViewFactoryCreator"/>

<bean id="mvcViewFactoryCreator" 
  class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers" ref="jspViewResolver"/>
</bean>

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

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

5. Navigating the Flows

5.流动的导航

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

6.结论

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.

你可以在GitHub上找到本文的完整源代码和所有代码片段