SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-001- 配置SpringFlow(flow-executor、flow-registry、FlowHandlerMapping、FlowHandlerAdapter)
一、
1.Wiring a flow executor
<flow:flow-executor id="flowExecutor" />
Although the flow executor is responsible for creating and executing flows, it’s not responsible for loading flow definitions. That responsibility falls to a flow registry,which you’ll create next.
2.Configuring a flow registry
A flow registry’s job is to load flow definitions and make them available to the flow executor.
(1)通配符<flow:flow-location-pattern>
1 <flow:flow-registry id="flowRegistry" base-path="/WEB-INF/flows"> 2 <flow:flow-location-pattern value="*-flow.xml" /> 3 </flow:flow-registry>
As declared here, the flow registry will look for flow definitions under the / WEB-INF /flows directory, as specified in the base-path attribute. Per the <flow:flow-location-pattern> element, any XML file whose name ends with -flow.xml will
be considered a flow definition.
All flows are referred to by their ID s.Using <flow:flow-location-pattern> as you have, the flow ID is the directory
path relative to the base-path —or the part of the path represented with the double asterisk
或者
1 <!-- The registry of executable flow definitions --> 2 <flow:flow-registry id="flowRegistry" base-path="/WEB-INF/flows"> 3 <flow:flow-location-pattern value="/**/*-flow.xml" /> 4 </flow:flow-registry>
(2)明确位置 <flow:flow-location>
1 <flow:flow-registry id="flowRegistry"> 2 <flow:flow-location path="/WEB-INF/flows/springpizza.xml" /> 3 </flow:flow-registry>
When configured this way, the flow’s ID is derived from the base name of the flow definition file, springpizza in this case.
或者指定id
1 <flow:flow-registry id="flowRegistry"> 2 <flow:flow-location id="pizza" path="/WEB-INF/flows/springpizza.xml" /> 3 </flow:flow-registry>
3.Handling flow requests
(1)装配FlowHandlerMapping,告诉DispatcherServlet,把flow交给它处理
DispatcherServlet typically dispatches requests to controllers. But for flows, you need a FlowHandlerMapping to help DispatcherServlet know that it should send flow requests to Spring Web Flow
1 <!--Maps request paths to flows in the flowRegistry--> 2 <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> 3 <property name="flowRegistry" ref="flowRegistry" /> 4 </bean>
As you can see, the FlowHandlerMapping is wired with a reference to the flow registry so it knows when a request’s URL maps to a flow. For example, if you have a flow whose ID is pizza , then FlowHandlerMapping will know to map a request to that flow if the request’s URL pattern (relative to the application context path) is /pizza.
(2)装配FlowHandlerAdapter
Whereas the FlowHandlerMapping ’s job is to direct flow requests to Spring Web Flow, it’s the job of a FlowHandlerAdapter to answer that call. A FlowHandlerAdapter is equivalent to a Spring MVC controller in that it handles requests coming in for a flow and processes those requests.
1 <!-- 2 Dispatches requests mapped to flows to FlowHandler implementations 3 --> 4 <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> 5 <property name="flowExecutor" ref="flowExecutor" /> 6 </bean>
This handler adapter is the bridge between DispatcherServlet and Spring Web Flow.It handles flow requests and manipulates the flow based on those requests. Here, it’s wired with a reference to the flow executor to execute the flows for which it handles requests.
二、所有的配置文件如下,webflow模块不支持java配置,只能用xml配置
1.
2.web.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?> 2 3 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 6 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>/WEB-INF/spring/root-config.xml</param-value> 10 </context-param> 11 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 16 <servlet> 17 <servlet-name>SpringPizza</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <init-param> 20 <param-name>contextConfigLocation</param-name> 21 <param-value></param-value> 22 </init-param> 23 <load-on-startup>1</load-on-startup> 24 </servlet> 25 26 <servlet-mapping> 27 <servlet-name>SpringPizza</servlet-name> 28 <url-pattern>/</url-pattern> 29 </servlet-mapping> 30 31 </web-app>
3.root-config.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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 7 8 <import resource="mvc.xml" /> 9 <import resource="flow.xml" /> 10 <import resource="services.xml" /> 11 <import resource="domain.xml" /> 12 <import resource="dataaccess.xml" /> 13 14 <context:component-scan base-package="com.springinaction.pizza" /> 15 16 </beans>
4.mvc.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" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 7 8 <mvc:annotation-driven /> 9 10 <!-- View resolver for the pizza flow, as shown on page 594 --> 11 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 12 <property name="prefix" value="/WEB-INF/jsp/" /> 13 <property name="suffix" value=".jsp" /> 14 </bean> 15 16 </beans>
5.flow.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" 4 xmlns:flow="http://www.springframework.org/schema/webflow-config" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/webflow-config 8 http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 13 14 <context:component-scan base-package="com.springinaction.pizza.flow" /> 15 16 <!-- Executes flows: the entry point into the Spring Web Flow system --> 17 <flow:flow-executor id="flowExecutor" /> 18 19 <!-- The registry of executable flow definitions --> 20 <flow:flow-registry id="flowRegistry" 21 base-path="/WEB-INF/flows"> 22 <flow:flow-location-pattern value="/**/*-flow.xml" /> 23 </flow:flow-registry> 24 25 <!--Maps request paths to flows in the flowRegistry--> 26 <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping"> 27 <property name="flowRegistry" ref="flowRegistry" /> 28 </bean> 29 30 <!-- 31 Dispatches requests mapped to flows to FlowHandler implementations 32 --> 33 <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"> 34 <property name="flowExecutor" ref="flowExecutor" /> 35 </bean> 36 37 </beans>
6.services.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 7 8 <bean id="pricingEngine" 9 class="com.springinaction.pizza.service.PricingEngineImpl" /> 10 11 <!-- 12 <lang:groovy id="pricingEngineGroovy" 13 script-source="classpath:scripts/PricingEngineImpl.groovy" /> 14 --> 15 16 <bean id="customerService" 17 class="com.springinaction.pizza.service.CustomerServiceImpl" /> 18 19 <!-- Payment processing bean, as discussed on page 606 --> 20 <bean id="paymentProcessor" 21 class="com.springinaction.pizza.service.PaymentProcessor" /> 22 23 <bean id="orderService" 24 class="com.springinaction.pizza.service.OrderServiceImpl" /> 25 26 </beans>
7.domain.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:spring-configured /> <bean id="order" class="com.springinaction.pizza.domain.Order" abstract="true"> <property name="pricingEngine" ref="pricingEngine" /> </bean> </beans>
8.dataaccess.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 7 </beans>