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

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

1.首先建立依赖,pom.xml文件

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.li</groupId>
  5     <artifactId>SpringWebFlow</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>SpringWebFlow Maven Webapp</name>
  9     <url>http://maven.apache.org</url>
 10     <dependencies>
 11         <dependency>
 12             <groupId>junit</groupId>
 13             <artifactId>junit</artifactId>
 14             <version>3.8.1</version>
 15             <scope>test</scope>
 16         </dependency>
 17         <!--SpringMVC所需要的依赖 -->
 18         <dependency>
 19             <groupId>org.springframework</groupId>
 20             <artifactId>spring-beans</artifactId>
 21             <version>4.3.7.RELEASE</version>
 22         </dependency>
 23
 24         <dependency>
 25             <groupId>org.springframework</groupId>
 26             <artifactId>spring-context</artifactId>
 27             <version>4.3.7.RELEASE</version>
 28         </dependency>
 29
 30         <dependency>
 31             <groupId>org.springframework</groupId>
 32             <artifactId>spring-core</artifactId>
 33             <version>4.3.7.RELEASE</version>
 34         </dependency>
 35
 36         <dependency>
 37             <groupId>org.springframework</groupId>
 38             <artifactId>spring-web</artifactId>
 39             <version>4.3.7.RELEASE</version>
 40         </dependency>
 41
 42         <dependency>
 43             <groupId>org.springframework</groupId>
 44             <artifactId>spring-webmvc</artifactId>
 45             <version>4.3.7.RELEASE</version>
 46         </dependency>
 47
 48         <dependency>
 49             <groupId>org.springframework</groupId>
 50             <artifactId>spring-expression</artifactId>
 51             <version>4.3.7.RELEASE</version>
 52         </dependency>
 53         <!--Spring Web Flow所需要的依赖 -->
 54         <dependency>
 55             <groupId>org.springframework.webflow</groupId>
 56             <artifactId>spring-binding</artifactId>
 57             <version>2.4.4.RELEASE</version>
 58         </dependency>
 59
 60         <dependency>
 61             <groupId>org.springframework.webflow</groupId>
 62             <artifactId>spring-faces</artifactId>
 63             <version>2.4.4.RELEASE</version>
 64         </dependency>
 65
 66         <dependency>
 67             <groupId>org.springframework.webflow</groupId>
 68             <artifactId>spring-js</artifactId>
 69             <version>2.4.4.RELEASE</version>
 70         </dependency>
 71
 72         <dependency>
 73             <groupId>org.springframework.webflow</groupId>
 74             <artifactId>spring-js-resources</artifactId>
 75             <version>2.4.4.RELEASE</version>
 76         </dependency>
 77
 78         <dependency>
 79             <groupId>org.springframework.webflow</groupId>
 80             <artifactId>spring-webflow</artifactId>
 81             <version>2.4.4.RELEASE</version>
 82         </dependency>
 83
 84         <dependency>
 85             <groupId>javax.servlet</groupId>
 86             <artifactId>servlet-api</artifactId>
 87             <version>2.5</version>
 88         </dependency>
 89
 90         <dependency>
 91             <groupId>javax.servlet</groupId>
 92             <artifactId>jstl</artifactId>
 93             <version>1.2</version>
 94         </dependency>
 95     </dependencies>
 96     <build>
 97         <finalName>SpringWebFlow</finalName>
 98         <plugins>
 99             <plugin>
100                 <groupId>org.apache.maven.plugins</groupId>
101                 <artifactId>maven-war-plugin</artifactId>
102                 <version>3.0.0</version>
103             </plugin>
104         </plugins>
105     </build>
106 </project>

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow="http://www.springframework.org/schema/webflow-config"
 4     xsi:schemaLocation=" http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6     http://www.springframework.org/schema/webflow-config
 7     http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd">
 8
 9     <!-- 流程注册器 隐含一句 flow-registry="flowRegistry"
10         默认表示引用bean id为 'flowRegistry'的流程注册表-->
11     <webflow:flow-executor id="flowExecutor" />
12
13     <!-- 流程注册表 -->
14     <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
15        <!--   <webflow:flow-location path="/WEB-INF/flows/hello.xml" id="hello" />-->
16         <webflow:flow-location path="/WEB-INF/customer/customer-flow.xml" id="customer" />
17     </webflow:flow-registry>
18
19     <!-- WebFlow 视图解析器 -->
20     <bean id="flowViewResolver"
21         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22          <property name="viewClass"
23             value="org.springframework.web.servlet.view.JstlView">
24         </property>
25         <property name="prefix" value="/WEB-INF/customer/">
26         </property>
27         <property name="suffix" value=".jsp">
28         </property>
29     </bean>
30
31     <!-- WebFlow 视图工厂构建服务 -->
32     <webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />
33
34     <!-- WebFlow 视图工厂创建器,表示使用视图解析器将流程配置(xml)中的逻辑视图交给视图解析器解析 → jsp -->
35     <bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
36         <property name="viewResolvers" ref="flowViewResolver" />
37     </bean>
38
39     <!-- 配置WebFlow 处理器映射器-->
40     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
41         <property name="mappings">
42             <props>
43                 <!-- 这个逻辑视图名的 前缀 必须与流程注册表中的
44                     webflow:flow-location 的 id一致,
45                     而 后缀 必须是当前DispatcherServlet匹配的地址,也就是
46                     必须以.flow结束,否则不被前端控制器处理(视图名必须匹配*.flow)
47                  -->
48                 <!-- 这里代表将请求路径为hello.flow的url交给flowController处理 -->
49                 <prop key="customer.flow">flowController</prop>
50             </props>
51         </property>
52     </bean>
53
54     <!--WebFlow 处理器,根据逻辑视图名到流程执行器中找到对应的注册表,进而找到流程配置文件,转到不同的物理视图-->
55     <!--主要工作就是负责将url转化成逻辑视图交给视图解析器解析 → jsp-->
56     <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
57         <property name="flowExecutor" ref="flowExecutor" />
58     </bean>
59 </beans>

3.web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <servlet>
 7         <servlet-name>FlowServlet</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <init-param>
10             <param-name>contextConfigLocation</param-name>
11             <param-value>
12         classpath:spring-wf.xml
13       </param-value>
14         </init-param>
15         <load-on-startup>1</load-on-startup>
16     </servlet>
17     <servlet-mapping>
18         <servlet-name>FlowServlet</servlet-name>
19         <url-pattern>*.flow</url-pattern>
20     </servlet-mapping>
21 </web-app>

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

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <flow xmlns="http://www.springframework.org/schema/webflow"
 3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4   xsi:schemaLocation="http://www.springframework.org/schema/webflow
 5   http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
 6
 7
 8
 9     <!-- Customer -->
10     <view-state id="welcome">
11         <transition on="phoneEntered" to="registrationForm"/>
12         <transition on="cancel" to="cancel"/>
13     </view-state>
14
15     <view-state id="registrationForm">
16         <transition on="submit" to="deliveryWarning" />
17         <transition on="cancel" to="cancel" />
18     </view-state>
19
20
21     <view-state id="deliveryWarning">
22         <transition on="accept" to="success" />
23         <transition on="cancel" to="cancel" />
24     </view-state>
25
26     <view-state id="success">
27         <transition on="back" to="cancel"></transition>
28     </view-state>
29
30     <!-- End state -->
31
32     <end-state id="cancel" view="externalRedirect:index.jsp"/>
33     <end-state id="customerReady" />
34 </flow>

5.index.jsp(首页)

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>My JSP 'cart.jsp' starting page</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26    <h2 align="center">Hello,WebFlow</h2><br/>
27       Spizza:<a href="customer.flow">进入Spizza应用</a><br/>
28   </body>
29 </html>

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11 <head>
12 <base href="<%=basePath%>">
13
14 <title>My JSP 'welcome.jsp' starting page</title>
15
16 <meta http-equiv="pragma" content="no-cache">
17 <meta http-equiv="cache-control" content="no-cache">
18 <meta http-equiv="expires" content="0">
19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20 <meta http-equiv="description" content="This is my page">
21 <!--
22     <link rel="stylesheet" type="text/css" href="styles.css">
23     -->
24
25 </head>
26
27 <body>
28     <h2>Welcome to Spizza!!!</h2>
29     <form:form>
30         <!-- 流程执行的key -->
31         <input type="hidden" name="_flowExecutionKey"
32             value="${flowExecutionKey}" />
33
34         <input type="text" name="phoneNumber" />
35         <br />
36         <!-- 触发phoneEntered事件 -->
37         <a href="${flowExecutionUrl}&_eventId=phoneEntered">Lookup
38             Customer</a>
39         &nbsp;&nbsp;&nbsp;&nbsp;
40         <a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
41         <br />
42     </form:form>
43 </body>
44 </html>

7.registrationForm.jsp(注册界面)

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 3 <html>
 4
 5   <head><title>Spring Pizza</title></head>
 6
 7   <body>
 8     <h2>Customer Registration</h2>
 9     <form>
10       <input type="hidden" name="_flowExecutionKey"
11              value="${flowExecutionKey}"/>
12       <b>Phone number: </b><input type='text'><br/>
13       <b>Name: </b><input type='text'><br/>
14       <b>Address: </b><input type='text'><br/>
15       <b>City: </b><input type='text'><br/>
16       <b>State: </b><input type='text'><br/>
17       <b>Zip Code: </b><input type='text'><br/>
18     </form>
19        <a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
20        &nbsp;&nbsp;&nbsp;&nbsp;
21        <a href="${flowExecutionUrl}&_eventId=cancel">Cancel</a>
22     </body>
23 </html>

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

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

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>My JSP 'success.jsp' starting page</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26     <h1>用户注册成功</h1>
27     <a href="${flowExecutionUrl}&_eventId=back">Home</a> |
28   </body>
29 </html>

9.运行结果

10.总结

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

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

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

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

转载于:https://www.cnblogs.com/lyj-gyq/p/9112545.html

笔记41 Spring Web Flow——Demo相关推荐

  1. Spring学习笔记之Spring Web Flow

    Spring Web Flow 是Spring MVC 的扩展,它支持开发基于流程的应用程序.它将流程的定义与实现流程行为的类和视图分离开来. 1.配置Web Flow 在Spring MVC上下文定 ...

  2. 笔记36 Spring Web Flow——配置

    Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序.Spring Web Flow是Spring MVC的扩展,它支持开发基于流程的应用程 序.它将流程的定义与实现流程行 ...

  3. Spring Web Flow 入门demo(三)嵌套流程与业务结合 附源码

    转载地址 ; http://blog.csdn.net/hejingyuan6/article/details/46723021 上篇博客我们说spring web Flow与业务结合的方式主要有三种 ...

  4. Spring Web Flow 入门demo(二)与业务结合 附源码

    转载地址 http://blog.csdn.net/hejingyuan6/article/details/46516287 第一部分demo仅仅介绍了简单的页面跳转,接下来我们要实现与业务逻辑相关的 ...

  5. Spring Web Flow 入门demo(一)简单页面跳转 附源码

    转载地址 http://blog.csdn.net/hejingyuan6/article/details/46508821 spring Web Flow (SWF)是Spring Framewor ...

  6. Spring Web Flow 入门demo(二)与业务结合 附源代码

    第一部分demo仅仅介绍了简单的页面跳转,接下来我们要实现与业务逻辑相关的功能. 业务的逻辑涉及到数据的获取.传递.保存.相关的业务功能函数的调用等内容,这些功能的实现都可用Java 代码来完毕,但定 ...

  7. Spring Web Flow 介绍

    <Spring实战>学习笔记-第八章:使用Spring Web Flow https://blog.csdn.net/bruce_6/article/details/51537263

  8. 用Spring Web Flow和Terracotta搭建Web应用

    什么是Spring Web Flow? Spring Web Flow是Spring Framework中的web应用组件,它提供了一种编写有状态和基于会话的web应用的简便手段.Spring Web ...

  9. MESSL(maven + extjs + spring portlet mvc + spring web flow + liferay )整合架构 5

    流控制文件很简单,就是根元素是<view>,然后用<view-state>来代表一个一个的页面,用<transition>来代表从一个状态到另外一个状态的跳转,如果 ...

最新文章

  1. 经典!工业界深度推荐系统与CTR预估必读的论文汇总
  2. 修正memcache.php中的错误示例
  3. Oracle SQL Trace 和 10046 事件 .
  4. Spring学习11-Spring管理各种数据源
  5. LeetCode Notes_#206 Reverse Linked List(C++,Python)
  6. python deque_python中deque类详解
  7. java 重载匹配_关于Java重载方法匹配优先级
  8. 在IT界取得成功应该知道的10件事(ZT)
  9. MySql库、表权限管理
  10. SpringBoot配置redis
  11. 08. 微信公众号生成带参数二维码
  12. 小学生遭校长拳击内脏出血 求医救命钱遭抢(图)
  13. Java基础【08】常用API——RandomAccessFile相关API
  14. Guitar Pro8.0吉他打谱作曲中文版有哪些新功能?
  15. RMAN CROSSCHECK命令 说明
  16. 快速实现ML302 GPS+4G发送定位信息详解
  17. 视野 | OpenSearch,云厂商的新选择?
  18. 肖战真的没我帅!我自己写的Python颜值检测说的!
  19. 考研科目计算机系统结构,计算机系统结构
  20. Window10 防火墙设置端口访问

热门文章

  1. oracle中$的用法,关于expdp 中query用法小结
  2. 四年级计算机笔试题,四年级计算机考试卷.doc
  3. php的Apa红灯_macos – 使用自制软件更新php和mysql后的apa...
  4. mysql 表 类型_mysql表类型
  5. 按120分计算成绩 mysql_Mysql实用教程试卷B.doc
  6. (并查集)Wireless Network
  7. java流读写_java流概述以及文件读写示例
  8. microstation添加txt文件_C开发实战-文件操作
  9. 三星s9android recovery,三星手机怎么进入recovery模式?详细图文教程指导
  10. php 邮箱开发教程,php开发中表单验证邮箱及URL的教程