Spring WebFlow(1)Introduce to web flow and Sample

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


你可能感兴趣的:(spring)