笔记41 Spring Web Flow——Demo

订购披萨的应用整体比较比较复杂,现拿出其中一个简化版的流程:即用户访问首页,然后输入电话号(假定未注册)后跳转到注册页面,注册完成后跳转到配送区域检查页面,最后再跳转回首页。通过这个简单的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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/570792.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CSS3弹性盒子Flex

CSS3弹性盒子Flex 基础知识和术语 原文链接&#xff1a;https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 父级&#xff08;flex容器&#xff09;的属性 &#xff03;显示 这定义了一个flex容器; 内联或块取决于给定的值。它为所有直接的孩子提供了一个弹性环境。 .co…

cloudera manager的7180 web界面访问不了的解决办法(图文详解)

说在前面的话 我的机器是总共4台&#xff0c;分别为ubuntucmbigdata1、ubuntucmbigdata2、ubuntucmbigdata3和ubuntucmbigdata4。&#xff08;注意啦&#xff0c;以下是针对Ubuntu系统的&#xff09; 在ubuntucmbigdata1上执行了 sudo apt-get install cloudera-manager-daemon…

2018 ios开发者账号同意新协议加联系电话教程

苹果开发者账号经常会更新协议&#xff0c;需要同意新的协议账号才能正常使用。 1、首先登录苹果开发者中心https://developer.apple.com/account/ 会出现下面飘红的提示&#xff0c;就是提示你要同意新协议。因为苹果规则的改变&#xff0c;需要先到appid管理中心加个联系手机…

Django REST FRAMEWORK swagger(一)框架详解

Django REST FRAMEWORK swagger&#xff08;一、框架详解&#xff09; 一.Django REST SWAGGER框架图 具体见下图 二.说明 RESTFul说明 每一个URI代表一种资源&#xff1b; 客户端和服务器之间&#xff0c;传递这种资源的某种表现层&#xff1b; 客户端通过四个HTTP动词&…

Prism for WPF初探(构建简单的模块化开发框架)

Prism for WPF初探&#xff08;构建简单的模块化开发框架&#xff09; 原文:Prism for WPF初探&#xff08;构建简单的模块化开发框架&#xff09;先简单的介绍一下Prism框架&#xff0c;引用微软官方的解释&#xff1a; Prism provides guidance to help you more easily desi…

15-[JavaScript]-ECMAScript 1

0.javaScript的发展历程 https://zhuanlan.zhihu.com/p/27985124 1、javaScript是什么&#xff1f; javaScript是一种web前端的描述语言&#xff0c;也是一种基于对象&#xff08;object&#xff09;和事件驱动&#xff08;Event Driven&#xff09;的、安全性好的脚本语言。 它…

WPF MVVM 架构 Step By Step(2)(简单的三层架构示例及粘合代码GLUE code)

WPF MVVM 架构 Step By Step(2)&#xff08;简单的三层架构示例及粘合代码GLUE code&#xff09; 原文:WPF MVVM 架构 Step By Step(2)&#xff08;简单的三层架构示例及粘合代码GLUE code&#xff09;我们第一步就是去了解三层架构和问题然后去看MVVM是怎么去解决这些问题的。…

基于YARN集群构建运行PySpark Application

文章转载&#xff1a;https://www.tuicool.com/articles/eaYVN3v Spark Application可以直接运行在YARN集群上&#xff0c;这种运行模式&#xff0c;会将资源的管理与协调统一交给YARN集群去处理&#xff0c;这样能够实现构建于YARN集群之上Application的多样性&#xff0c;比…

Apache nifi 集群安装

原文地址&#xff1a;https://pierrevillard.com/2016/08/13/apache-nifi-1-0-0-cluster-setup/ 文章写的很好了&#xff0c;步骤性的英文写得也比较易懂&#xff0c;原样搬过来了&#xff0c;没有再翻译 As you may know a version 1.0.0-BETA of Apache NiFi has been rele…

Html5 学习笔记 --》html基础 css 基础

HTML5 功能 HTML5特点 <!DOCTYPE html> <html lang"zh-cn"> <head><meta charset"utf-8"><title>基本格式</title> </head> <body><a href"http://www.baidu.com">百度</a> </b…

VMware仅主机模式访问外网

原文转载至&#xff1a;https://blog.csdn.net/eussi/article/details/79054622 保证VMware Network Adapter VMnet1是启用状态 将可以连接外网的连接共享属性设置成如下图所示 将VMware Network Adapter VMnet1的IP地址设置成与本机IP不同的网段即可 VMware虚拟网络编辑器VMne…

IE上ORACLE OEM 证书错误 , 导航阻止,无法”继续浏览此网站”

文章转载自&#xff1a;http://blog.51cto.com/cswggod/1193266 仅用于个人学习&#xff0c;知识收藏 本文是我安装ORACLE11g后客户端IE访问不了是出现的&#xff0c;无奈下找OTN上help&#xff0c; 结果很lucky的被解脱了。 网站是&#xff1a;https://forums.oracle.com/for…

testng使用DataProvider+Excel实现DDT

DDT&#xff0c;即数据驱动测试 Data Driver Test&#xff0c;我曾经记录了一篇关于python的DDT框架&#xff08;ExcelDDT数据驱动实例&#xff09;&#xff0c;那么java中的DDT是怎么样的呢&#xff1f;在java中&#xff0c;可以用testng的DataProvider和Excel实现。 首先建一…

Linux安装Oracle12C 过程及遇到的问题

一、环境介绍 1、系统环境&#xff1a;CentOS7.1 Oracle版本&#xff1a;12C 12.1.0 二、安装过程 1、安装过程文档见百度云上的文档 链接&#xff1a;https://pan.baidu.com/s/1nvd07NF 密码&#xff1a;mey9 2、安装完后登录数据库 su oracle source ~/.bash_profiel…

云监控 Ganglia 安装步骤 (含python module)

文章转载自&#xff1a;https://my.oschina.net/duangr/blog/181585 &#xff0c;仅用于个人学习、收藏&#xff0c;转载请注明原作者地址。 前言 最近在研究云监控的相关工具,感觉ganglia颇有亮点,能从一个集群整体的角度来展现数据. 但是安装过程稍过复杂,相关依赖稍多…

ORA-65096: 公用用户名或角色名无效引发的思考

解决方式&#xff1a; alter session set "_ORACLE_SCRIPT"true; alter session set containerPDBORCL;原因&#xff1a;查官方文档得知“试图创建一个通用用户&#xff0c;必需要用C##或者c##开头”&#xff0c;这时候心里会有疑问&#xff0c;什么是common user&am…

VS封装给Unity使用的DLL

首先: 注意两点,一个是选择3.5,Unity最高支持到3.5 然后要选择第二个FrameWork类库 第一个会报错 然后导入Unity dll 我Unity安装在F:\AppLicationWorkSpace\Unity5.6.2\Unity\Editor\Data\Managed 用哪个导入哪个 然后生成 Ok 把生成的DLL放到Unity里就可以使用了 继续写…

hawq state 报错: the database is down, but Ambari shows all hawq services as being

此问题官方有给出解决方案&#xff1a;https://discuss.pivotal.io/hc/en-us/articles/221826748-Pivotal-HDB-state-indicates-the-database-is-down-but-Ambari-shows-all-Pivotal-HDB-services-as-being-up Environment ProductVersionPivotal HDB (HAWQ)2.x Symptom Piv…

OpenLayer学习之矢量地图

一、首先了解下矢量地图和栅格地图 矢量图使用直线和曲线来描述图形&#xff0c;这些图形的元素是一些点、线、矩形、多边形、圆和弧线等等&#xff0c;矢量地图放大和缩小不会失真&#xff08;图片你要是放大一定程度明显可以看出一个一个小格→栅格地图的缺点&#xff09;。为…

文件拷贝(字符、字节)

1.字节流byte&#xff1a;读入到字节数组后&#xff0c;返回一个长度len&#xff0c;如果没有读到数据&#xff0c;len-1 2.字符流char&#xff1a;同样是-1 3.代码生成器&#xff1a;null 一行一行地读 4.键盘录入&#xff0c;写入文件 5.构造器&#xff0c;追加用true 6.类…