订购披萨的应用整体比较比较复杂,现拿出其中一个简化版的流程:即用户访问首页,然后输入电话号(假定未注册)后跳转到注册页面,注册完成后跳转到配送区域检查页面,最后再跳转回首页。通过这个简单的Demo用来说明Spring Web Flow的具体工作流程,方便以后细化整个订购披萨应用。

基于Maven的项目,目录结构如下:

1.首先建立依赖,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.li</groupId>
<artifactId>SpringWebFlow</artifactId>
<packaging>war</packaging>
<version>0.0.1-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>
<!--SpringMVC所需要的依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--Spring Web Flow所需要的依赖 -->
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-binding</artifactId>
<version>2.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-faces</artifactId>
<version>2.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-js</artifactId>
<version>2.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-js-resources</artifactId>
<version>2.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringWebFlow</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>

2.Spring Web Flow配置文件spring-wf.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd"> <!-- 流程注册器 隐含一句 flow-registry="flowRegistry"
默认表示引用bean id为 'flowRegistry'的流程注册表-->
<webflow:flow-executor id="flowExecutor" /> <!-- 流程注册表 -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<!-- <webflow:flow-location path="/WEB-INF/flows/hello.xml" id="hello" />-->
<webflow:flow-location path="/WEB-INF/customer/customer-flow.xml" id="customer" />
</webflow:flow-registry> <!-- WebFlow 视图解析器 -->
<bean id="flowViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView">
</property>
<property name="prefix" value="/WEB-INF/customer/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean> <!-- WebFlow 视图工厂构建服务 -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" /> <!-- WebFlow 视图工厂创建器,表示使用视图解析器将流程配置(xml)中的逻辑视图交给视图解析器解析 → jsp -->
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="flowViewResolver" />
</bean> <!-- 配置WebFlow 处理器映射器-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 这个逻辑视图名的 前缀 必须与流程注册表中的
webflow:flow-location 的 id一致,
而 后缀 必须是当前DispatcherServlet匹配的地址,也就是
必须以.flow结束,否则不被前端控制器处理(视图名必须匹配*.flow)
-->
<!-- 这里代表将请求路径为hello.flow的url交给flowController处理 -->
<prop key="customer.flow">flowController</prop>
</props>
</property>
</bean> <!--WebFlow 处理器,根据逻辑视图名到流程执行器中找到对应的注册表,进而找到流程配置文件,转到不同的物理视图-->
<!--主要工作就是负责将url转化成逻辑视图交给视图解析器解析 → jsp-->
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
</beans>

3.web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>FlowServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-wf.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FlowServlet</servlet-name>
<url-pattern>*.flow</url-pattern>
</servlet-mapping>
</web-app>

4.流程注册文件,customer-flow.xml

 <?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"> <!-- Customer -->
<view-state id="welcome">
<transition on="phoneEntered" to="registrationForm"/>
<transition on="cancel" to="cancel"/>
</view-state> <view-state id="registrationForm">
<transition on="submit" to="deliveryWarning" />
<transition on="cancel" to="cancel" />
</view-state> <view-state id="deliveryWarning">
<transition on="accept" to="success" />
<transition on="cancel" to="cancel" />
</view-state> <view-state id="success">
<transition on="back" to="cancel"></transition>
</view-state> <!-- End state --> <end-state id="cancel" view="externalRedirect:index.jsp"/>
<end-state id="customerReady" />
</flow>

5.index.jsp(首页)

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'cart.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h2 align="center">Hello,WebFlow</h2><br/>
Spizza:<a href="customer.flow">进入Spizza应用</a><br/>
</body>
</html>

6.welcome.jsp(输入电话的页面)

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'welcome.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h2>Welcome to Spizza!!!</h2>
<form:form>
<!-- 流程执行的key -->
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}" /> <input type="text" name="phoneNumber" />
<br />
<!-- 触发phoneEntered事件 -->
<a href="${flowExecutionUrl}&_eventId=phoneEntered">Lookup
Customer</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
<br />
</form:form>
</body>
</html>

7.registrationForm.jsp(注册界面)

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html> <head><title>Spring Pizza</title></head> <body>
<h2>Customer Registration</h2>
<form>
<input type="hidden" name="_flowExecutionKey"
value="${flowExecutionKey}"/>
<b>Phone number: </b><input type='text'><br/>
<b>Name: </b><input type='text'><br/>
<b>Address: </b><input type='text'><br/>
<b>City: </b><input type='text'><br/>
<b>State: </b><input type='text'><br/>
<b>Zip Code: </b><input type='text'><br/>
</form>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
</body>
</html>

8.deliveryWarning.jsp(配送区域检查页面)

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>Spring Pizza</title></head> <body>
<h2>Delivery Unavailable</h2> <p>The address is outside of our delivery area. The order
may still be taken for carry-out.</p> <a href="${flowExecutionUrl}&_eventId=accept">Accept</a> |
<a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
</body>
</html>

9.success.jsp(用户添加成功页面)

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>用户注册成功</h1>
<a href="${flowExecutionUrl}&_eventId=back">Home</a> |
</body>
</html>

9.运行结果

10.总结

相同点:SpringMVC和Spring Web Flow都实现了mvc设计模式

不同点:在mvc设计模式的实现方面不同

SpringMVC通过编写controller,service类来实现

而流程则通过bean来实现,底层已经帮你实现了,帮你来处理请求跳转到对应的视图界面

最新文章

  1. string类型转换int类型
  2. noi 04:求整数的和与均值
  3. 应用jacob组件造成的内存溢出解决方案(java.lang.OutOfMemoryError: Java heap space)
  4. 安装 Oracle P6 EPPM 16 R1 database for 12C A
  5. [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard
  6. 正确理解SQL Server的许可证(转)
  7. 中文版kendoUI API — Grid(一)
  8. cocos2d-x游戏开发系列教程-坦克大战游戏启动界面的编写
  9. Linux内核开发之将驱动程序添加到内核
  10. [C++学习历程]基础部分 C++中的函数中的值参数、地址参数、引用参数实际例子
  11. 【Android Studio安装部署系列】十四、Android studio移除工程和删除项目
  12. 【学习总结】GirlsInAI ML-diary day-14-function函数
  13. 认识jmeter
  14. cad.net 利用win32api实现不重复打开dwg路径的文件夹(资源管理器)
  15. CSS3渐变——线性渐变
  16. ActiveMQ学习笔记1
  17. 数据库 - Navicat与pymysql模块
  18. scala-02-数组的操作
  19. CSS-3 圆角Border-radius 的使用
  20. ( ( (int(*)(uint, ushort, uint *, uint, int)) (*((uint *)(TCM_BASE + 0x8))) ) (a,b,c,d,e) )

热门文章

  1. Neo4j parameter
  2. 前端移动端开发总结(Vue)
  3. python_django_类视图的第一次忐忑碰触!!!
  4. python_django_分页
  5. css自适应问题
  6. 【JZOJ3674】【luoguP4042】【BZOJ3875】骑士游戏
  7. sql 条件查询
  8. BZOJ 3159: 决战 解题报告
  9. 「CTS2019 | CTSC2019」随机立方体 解题报告
  10. BZOJ 4516: [Sdoi2016]生成魔咒(后缀数组)