這段時間在看《Spring 實戰》里面有講Spring Web Flow,覺得里面的例子過於復雜,不適合新手,於是在網上找了個例子,跟着寫
以下是項目的目錄,我是基於maven搭建項目的
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>SpringWebFlow</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> <!--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> </project>
Spring的配置文件
<?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-2.0.xsd"> <!-- 創建流程執行器,默認表示引用bean id為 'flowRegistry'的流程注冊表--> <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/> <!-- 注冊流程注冊表 --> <webflow:flow-registry id="flowRegistry"> <webflow:flow-location path="/WEB-INF/flows/hello.xml" id="hello" /> </webflow:flow-registry> <!--作用:幫助DispatcherServlet將流程請求發送給Spring Web Flow--> <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> <property name="flowRegistry" ref="flowRegistry"/> </bean> <!--作用:等同於controller,它會響應發送的流程請求並對其進行處理--> <bean id="flowHandlerAdapter" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> <property name="flowExecutor" ref="flowExecutor"/> </bean> </beans>
此時流程注冊表和jsp文件在同一個文件夾下面,所以不需要配置視圖解析器和視圖工廠
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>
流程注冊表
<?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"> <view-state id="cart" > <transition on="submit" to="order"> </transition> </view-state> <view-state id="order"> <transition on="confirm" to="finish"> </transition> </view-state> <view-state id="finish"> <transition on="toindex" to="index"> </transition> </view-state> <end-state id="index" view="externalRedirect:index.jsp"> </end-state> </flow>
<view-state>,如果只有id屬性的時候,會在流程文件所在的文件夾尋找id名字的jsp文件作為view。如果明確指定view屬性的話,就找這個名字的jsp文件
各種jsp文件
index.jsp
<%-- Created by IntelliJ IDEA. User: I am master Date: 2017/5/2 Time: 14:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2 align="center">Hello,WebFlow</h2><br/> Item1:<a href="hello.flow">加入購物車</a><br/> Item2:<a href="hello.flow">加入購物車</a><br/> Item2:<a href="hello.flow">加入購物車</a> </body> </html>
cart.jsp
<%-- Created by IntelliJ IDEA. User: I am master Date: 2017/5/2 Time: 14:43 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2 align="center">購物車</h2><br/> 衣服:<a href="${flowExecutionUrl}&_eventId=submit">買買買!!!</a><br/> 褲子:<a href="${flowExecutionUrl}&_eventId=submit">買買買!!!</a><br/> 鞋子:<a href="${flowExecutionUrl}&_eventId=submit">買買買!!!</a> </body> </html>
order.jsp
<%-- Created by IntelliJ IDEA. User: I am master Date: 2017/5/2 Time: 14:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2 align="center">訂單</h2><br/> <a href="${flowExecutionUrl}&_eventId=confirm">確認支付</a> </body> </html>
finish.jsp
<%-- Created by IntelliJ IDEA. User: I am master Date: 2017/5/2 Time: 14:45 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2 align="center">確認支付</h2><br/> <a href="${flowExecutionUrl}&_eventId=toindex">恭喜你,支付成功..</a> </body> </html>
運行結果
遇到的問題:
自己寫完以后,運行不起來,報404
表達式沒有解析,一開始以為是缺少jar包,胡亂加了很多不必要的jar包,但是還是沒有解決
錯誤原因:maven搭建項目web.xml文件默認的版本是2.3,但是我們需要的是3.1,因此將web.xml改為3.1的版本就可以了
很多情況下,我們會將流程注冊表和jsp放在不同的文件夾下,這就需要視圖解析器和視圖工廠
則Spring的配置文件應該為如下
<?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-2.0.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-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/flowView/"> </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="hello.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>
小結:
相同點:SpringMVC和流程都實現了mvc設計模式
不同點:在mvc設計模式的實現方面不同
SpringMVC通過編寫controller,service類來實現
而流程則通過bean來實現,底層已經幫你實現了,幫你來處理請求跳轉到對應的視圖界面