《Spring Web Flow 实践》
在《Spring In Action》这本书上的第八章介绍了Spring Web Flow的相关知识。
在《Spring In Action》这本书上是这样介绍的:Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序。比较典型的例子就是电子商务站点的结账流程,从购物车开始,应用程序会引导用户依次经过派送详情、账单信息以及最终的订单确认流程。
关于Spring Web Flow,官网上的介绍是这样的:
Spring Web Flow builds on Spring MVC and allows implementing the “flows” of a web application. A flow encapsulates a sequence of steps that guide a user through the execution of some business task. It spans multiple HTTP requests, has state, deals with transactional data, is reusable, and may be dynamic and long-running in nature..
简单的翻译下:Spring Web Flow是建立在Spring MVC上面的,并允许实现web应用程序的“流”。这个流封装了一系列用于指导用户执行业务任务的步骤。它跨越多个HTTP请求,具有状态,可以处理事务数据,而且是可重用的,并且可能是动态的和长期运行的。
和参考资料所列出来的博客一样,本篇博文将按照购物车实例作为例子来学习并实践了下,记录如下。
购物车流程如下所示。
Spring Web Flow购物车实例
具体步骤如下:
1、使用IDEA创建一个新的Spring MVC项目,并添加相关的依赖,pom.xml文件中的内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wrh</groupId>
<artifactId>springwebflow</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springwebflow Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.4.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>springwebflow</finalName>
</build>
</project>
2、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>winner-test Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatch