spring web flow 2 集成spring mvc和velocity

首先在web.xml加以下代码

<!-- Character Encoding filter -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>GBK</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- spring web flow -->
	<servlet>
		<servlet-name>WebFlow</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:webflow-config.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>WebFlow</servlet-name>
		<url-pattern>/payflow/*</url-pattern>
	</servlet-mapping>

添加webflow-config.xml

<?xml version="1.0" encoding="GBK"?>
<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" />
	<!-- The registry of executable flow definitions -->
	<webflow:flow-registry id="flowRegistry"
		flow-builder-services="flowBuilderServices">
		<webflow:flow-location path="/WEB-INF/flows/payflow.xml"
			id="payflow" />
		<webflow:flow-location path="/WEB-INF/flows/readvalue.xml"
			id="readvalue" />
	</webflow:flow-registry>

	<webflow:flow-builder-services id="flowBuilderServices"
		view-factory-creator="mvcViewFactoryCreator" />
	<!--
		Configures Web Flow to use velocity to create views for rendering
	-->
	<bean id="mvcViewFactoryCreator"
		class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
		<property name="viewResolvers" ref="velocityViewResolver" />
	</bean>

	<!-- view support -->

	<!-- velocity view begin 使用VELOCITY-->
	<bean id="velocityConfig"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="resourceLoaderPath" value="/templates/screen/" />
		<property name="velocityProperties">
			<props>
				<prop key="input.encoding">GBK</prop>
				<prop key="output.encoding">GBK</prop>
			</props>
		</property>
	</bean>

	<bean id="velocityViewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<property name="cache" value="true" />
		<property name="contentType" value="text/html;charset=GBK" />
		<property name="exposeSpringMacroHelpers" value="true" />
	</bean>

	<!-- webmvc-config -->
	<bean id="viewMappings"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<value>
				/payflow.htm=flowController
			</value>
		</property>
		<property name="defaultHandler">
			<!-- UrlFilenameViewController会将"/index"这样的请求被映射成名为"index"的视图-->
			<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
		</property>
	</bean>

	<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
		<property name="flowExecutor" ref="flowExecutor" />
	</bean>
</beans>

添加payflow.xml和readvalue.xml(subflow)

<?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">

	<var name="payflow" class="com.taobao.upp.mpc.web.action.PayFlow" />
	
	<on-start>
		<evaluate expression="payflow.setBean(requestParameters)" result="conversationScope.flow"></evaluate>
	</on-start>
	
	<view-state id="start" view="invocation.vm" popup="false" redirect="false">
		<transition on="submit" to="regist_view" bind="false" />
	</view-state>
	<subflow-state id="readvaluess" subflow="readvalue">
		<transition on="readover" to="regist_view"></transition>
	</subflow-state>
	<view-state id="unregist_view" view="user-quick-buy"
		redirect="false" popup="false" />

	<view-state id="regist_view" view="user-alipay-binding"
		redirect="false" popup="false">
		<on-render>
			<evaluate expression="flow.readValue()" result="flowScope.value" />
		</on-render>
		<transition on="readvalues" to="readvaluess"></transition>
		<transition on="delivervalues" to="showvalue"></transition>
	</view-state>

	<view-state id="showvalue" view="user-tb-login" redirect="false"
		popup="false">
	</view-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">

	<action-state id="readvalue">
		<evaluate expression="payflow.readValue()" />
		<transition to="readover" />
	</action-state>
	<end-state id="readover" />
</flow>

这样就完全OK了,velocity的编码问题亦一同搞定了!如何用就多学习相关资料了

相关推荐