Spring Web Flow入门(带详细注释)

越季萌
2023-12-01

1.构建webflow2.3.3环境(2.0以上应该都可以)

导入webflow的jar包+springmvc的一些必须包,目录结构如下

2.配置webflow(spring-wf.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: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>


3.配置WebFlow前端控制器(web.xml)

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


拦截所有.flow结尾的请求交给springmvc前端控制器处理

至此,webflow需要的所有组件都已经配置完成,现在还有2步,配置流程文件+跳转页面

4.流程配置文件(hello.xml[/WEB-INF/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">
	<view-state id="cart" view="cart">
		<transition on="submit" to="order">
		</transition>
	</view-state>
	<view-state id="order" view="order">
		<transition on="confirm" to="finish">
		</transition>
	</view-state>
	<view-state id="finish" view="finish">
		<transition on="toindex" to="index">
		</transition>
	</view-state>
	<end-state id="index" view="externalRedirect:index.jsp">
	</end-state>
</flow>


流程配置文件首先执行第一个状态标签。

5.跳转页面

index.jsp(根目录)

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</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(/WEB-INF/flowView/)

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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(/WEB-INF/flowView/)

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>订单</title>
</head>
<body>
	<h2 align="center">订单</h2>
	<br/>
	<a href="${flowExecutionUrl}&_eventId=confirm">确认支付</a>
</body>
</html>


 

finish.jsp(/WEB-INF/flowView/)

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>view order</title>
</head>
<body>
	<h2 align="center">确认支付</h2>
	<br/>
	<a href="${flowExecutionUrl}&_eventId=toindex">恭喜你,支付成功..</a>
</body>
</html>

${flowExecutionUrl}由webflow框架赋值,其中包含了flow各个状态的值(动态生成,每个状态不一样),以及对应的model数据

_eventId的值与本状态transition标签的on属性值对应,通过这个值跳转到不同的状态或者视图,实现转移

 类似资料: