Spring WebFlow(1)Introduce to web flow and Sample

本文介绍了Spring WebFlow的基本概念、安装步骤、配置方法及其实现案例,包括使用Spring WebFlow处理多请求间的数据和状态,与Spring Security的集成,以及如何在实际应用中创建、配置和执行WebFlow。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring WebFlow(1)Introduce to web flow and Sample

1. Install the flow edit plugin in STS
Eclipse plugin URL http://springide.org/updatesite
I only install the webflow related plugins.

2. Spring Web Flow 2.0 Introduce
The goal for this tool is to deal with the data/states across multi requests.
Spring Web Flow 1.x is a standalone tool supported Spring Web MVC, struts, JSF and etc. But the latest 2.0 is only for Spring Web MVC.
Web Flow can be cooperate with Spring Security with 'secured'.

3. Take Add to Cart Case as example
add this to my pom.xml
<!-- spring snapshot -->
<repositories>
<repository>
<id>springsource-repository</id>
<name>Spring Maven Releases, Milestones, and Snapshots</name>
<url>http://repo.springsource.org/snapshot</url>
</repository>
</repositories>

And the dependency:
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>

Create a Web Flow Definition file order.xml:
<view-state id="viewCart">
<transition on="submit" to="viewOrder" />
</view-state>
<view-state id="viewOrder">
<transition to="viewConfirmed" on="confirm"></transition>
</view-state>
<view-state id="viewConfirmed">
<transition to="returnToIndex" on="returnToIndex"></transition>
</view-state>
<end-state id="returnToIndex"></end-state>

There are 5 types of states in Web Flow.
Action State,
View State,
Subflow State,
Decision State,
End State.

4. What Spring Web Flow brings to us
There are 3 typical scope in web development.
Session,
Application.
Request.

Spring Web Flow brings us 2 more:
flow: create while flow creation, end in flow destroyment, we can visit the data via 'flowScope'
conversation: mostly same as flow, but when the flow call a subflow, we can visit the data in subflow.

We have flow and conversation, they are useful but that will make the server slow.

5. Configure the Spring Web Flow 2.0 Basic Configuration
FlowRegistry is the repository of the flows. Every xml file of flow will be stored in FlowRegistry as FlowDefinition with key of ID.
FlowExecutor
This is the bean which will execute the flow.
Which flow will be executed? This will be decided by the URL and the flow Id.
http://localhost:8080/easywebflow/shopping will identify the flow with id shopping.
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="classpath:flows/shopping.xml"
id="shopping" />
<webflow:flow-location path="classpath:flows/addToCart.xml"
id="addToCart" />
</webflow:flow-registry>

<webflow:flow-executor id="flowExecutor" />


Spring Web Flow and Spring Web MVC work together
Spring Web MVC Process
Client send requests -----> Servlet Container(Tomcat) ------> Our Web Application(easywebflow) ------> Application Servlet(spring servlet) -------> DispatcherServlet (Spring IOC HandlerMapping) ------> Controller ------>View

There should be a handler/controller in HandlerMapping to deal with flow requests.

FlowHandler and FlowController

FlowBuilder Services

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name = "flowExecutor" ref="flowExecutor" />
</bean>

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
<property name="order" value="1"/>
</bean>

<webflow:flow-builder-services id="flowBuilderServices"
view-factory-creator="mvcViewFactoryCreator" />

<bean id="mvcViewFactoryCreator"
class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="velocityViewResolver" />
</bean>

Use Unified EL to Implement the Logic
When will the logic code will be execute?
1. client sumibt the request include _eventId paramter.
2. invoke the framework point
3. execute the <action-state> element

Request consist of _eventId Parameter
<transition on="submit">
<evaluate expression="validator.validate()" />
</transition>
once submit is clicked, we will call the object validator method validate().

Invoke the Framework Point
flow start <on-start>
state entry <on-entry>
view render <on-render>
state exit <on-exit>
flow end <on-end>

<view-state id="viewCart" view="viewCart">
<on-render>
<evaluate expression="productService.getProducts()" result="viewScope.products" />
</on-render>
</view-state>

And we can get the viewScope data in the JSP files.

Execute the <action-state> Element
<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))" />
<transition to="productAdded" />
</action-state>

How to Save the Data
flowScope
<evaluate expression="productService.getProducts()" result="flowScope.products" />
viewScope
requestScope
conversationScope

Use Subflow to Add To Cart
<view-state id="viewCart" view="viewCart">
<on-render>
<evaluate expression="productService.getProducts()" result="viewScope.products"/>
</on-render>
<transition on="submit" to="viewOrderDecistion">
</transition>
<transition on="addToCart" to="addProductToCart" />
</view-state>
<subflow-state id="addProductToCart" subflow="addToCart">
<transition on="productAdded" to="viewCart">
</transition>
</subflow-state>

Global Transition
we can call global transition in every state.
<global-transitions>
<transition on="cancelShopping" to="returnToIndex" />
</global-transitions>

This is only a easy sample, use serviceBean in webflow.xml configuration file, use subflow.

references:
http://www.springsource.org/spring-web-flow
http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/index.html
http://www.ibm.com/developerworks/cn/education/java/j-spring-webflow/index.html
http://www.ibm.com/developerworks/cn/education/java/j-spring-webflow/section5.html
http://www.jpalace.org/docs/tutorials/spring/webflow_18.html
http://wutaoo.iteye.com/blog/415899
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值