首页 > 代码库 > Spring web flow 配置文件
Spring web flow 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=http://www.mamicode.com/"/"/>
<property name="suffix" value=http://www.mamicode.com/".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index">index1</prop>
<prop key="/shopping">flowController</prop>
</props>
</property>
</bean>
<bean id="index1" class="org.springframework.web.servlet.mvc.ParameterizableViewController" >
<property name="viewName" value=http://www.mamicode.com/"index">
</bean>
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<!--FlowController可以理解为时flow的入口,也就是spring mvc与spring web flow的结合点,将请求交给词控制器处理就表示进入到flow中去,具体进入到哪一个flow就是靠flowExecutor来判断,判断的方式是根据请求的URL,那么在本例中将会进入到id为shopping的flow-->
</beans>
<?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">
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />
<!-- 所有 flow的定义文件它的位置在这里进行配置, flow-builder-services 用于配置 flow 的特性 -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/shopping.xml" id="shopping" />
<webflow:flow-location path="/WEB-INF/flows/addToCart.xml" id="addToCart" />
</webflow:flow-registry>
<!--Web Flow 中的视图通过 MVC 框架的视图技术来呈现 -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />
<!-- 指明 MVC 框架的 view resolver ,用于通过 view 名查找资源 -->
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver" />
</bean>
</beans>
<?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中的view对应jsp文件夹中的jsp页面,on是触发事件,to对应state id -->
<!-- <var name="mycart" class="bean.Cart"/> 表示定义一个名为mycart的变量,其值为bean.Cart类对象
这种方式可以使用spring注解代替:@Component("mycart")
-->
<on-start> <!-- 表示在进入此flow之前执行 -->
<set name="conversationScope.cart" value=http://www.mamicode.com/"mycart">
</on-start>
<view-state id="viewCart" view="/jsp/viewCart" > <!-- 我们可以看到这是此flow的第一个state,那么当进入到这个flow时 -->
<on-render> <!--在这个视图渲染之前,执行里面的业务逻辑--> <!--首先执行这个state,即进入到viewCart页面 -->
<evaluate expression="productService.getProducts()" result="viewScope.products"/>
<!-- expression表达式表示业务逻辑,将该方法的返回值放入到view范围内的products中 -->
</on-render>
<transition on="submit" to="viewOrder"><!--当在此状态下,如果触发submit,则会转向viewOrder状态 -->
</transition>
<transition on="addToCart" to="addProductToCart"/>
</view-state>
<view-state id="viewOrder" view="/jsp/viewOrder">
<transition on="confirm" to="orderConfirmed">
</transition>
</view-state>
<view-state id="orderConfirmed" view="/jsp/viewConfirmed">
<transition on="returnToIndex" to="returnToIndex">
</transition>
</view-state>
<end-state id="returnToIndex" view="index">
</end-state><!-- 次flow的结束状态 ,不能再转向其他状态-->
<subflow-state id="addProductToCart" subflow="addToCart"> <!-- 子流,subflow表示子flow的id -->
<transition on="productAdded" to="viewCart" />
</subflow-state>
</flow>
<?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">
<on-start>
<set name="requestScope.productId" value=http://www.mamicode.com/"requestParameters.productId"/>
</on-start>
<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))"/>
<transition to="productAdded"/>
</action-state> <!-- 此state就是用来处理业务逻辑的 -->
<end-state id="productAdded"/>
</flow>
Spring web flow 配置文件