轉載注明出處http://www.cnblogs.com/wdfwolf3/p/6553545.html
web.xml文件和springmvc-servlet文件按照常規配置,不再贅述。修改Spring配置文件application-context.xml,添加下列代碼
<flow:flow-executor id="flowExecutor" />
<!--方法1:此時id為base-path和flow流程文件名中間的部分。如果流程文件*-flow.xml路徑為/WEB-INF/flows/test/wdfwolf3-flow.xml, 那么id為/test,訪問的url即為/test;這里有個問題就是如果valut=“/*-flow.xml”這樣寫的話base-path和.xml文件中間不能有路徑了,此時
id為文件名,類似於第二種方法--> <flow:flow-registry id="flowRegistry" base-path="WEB-INF/flows"> <flow:flow-location-pattern value="/**/*-flow.xml" /> </flow:flow-registry> <!--方法2:此時id為flow文件名,即test。訪問的url即為/testflow--> <!--<flow:flow-registry id="flowRegistry"> <flow:flow-location path="WEB-INF/flows/testflow.xml"/> </flow:flow-registry>--> <!--方法3:此時id自定義指定為test。訪問的url即為/test--> <!--<flow:flow-registry id="flowRegistry"> <flow:flow-location id="test" path="WEB-INF/flows/testflow.xml"/> </flow:flow-registry>-->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping" > <property name="flowRegistry" ref="flowRegistry" /> </bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> <property name="flowExecutor" ref="flowExecutor"/> </bean>
這里也可以將flow的配置單獨建一個文件,然后在application-context中<import resource="flow配置文件" />。
由於本文講的是配置,所以不講解webflow的使用。舉個簡單的例子,主要說明一下view的映射,當訪問/url請求時,如果DispatcherServlet找不到對應的處理器controller,就會交給Web Flow,這樣Web Flow就會按照流程定義開始流程。來看一下流程文件testflow.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.4.xsd" > <view-state id="viewCart" view="viewCart.jsp"> <transition on="submit" to="viewOrder"> </transition> </view-state> <view-state id="viewOrder"> <transition on="returnToIndex" to="returnToIndex"> </transition> </view-state> <end-state id="returnToIndex" view="externalRedirect:servletRelative:/index.jsp"> </end-state> </flow>
<view-state>,如果只有id屬性的時候,會在流程文件所在的文件夾尋找id名字的jsp文件作為view。如果明確指定view屬性的話,就找這個名字的jsp文件。在調試過程中遇到的兩個坑:
1.view屬性中文件名需要寫上.jsp,不然找不到。
2.具體在哪里找jsp文件,官方文檔(下圖)中有說明,即“in the directory where the flow is located”,流程所在文件夾。最后是幾個jsp文件
viewCart.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>View Cart</title> </head> <body> <h1>View Cart</h1> <a href="${flowExecutionUrl}&_eventId=submit">Submit</a> </body> </html>
viewOrder.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>View Order</h1> <a href="${flowExecutionUrl}&_eventId=returnToIndex">Return to index</a> </body> </html>