| By Kishore Kumar | Article Rating: |
|
| December 4, 2005 03:15 AM EST | Reads: |
45,790 |
A FlowExecutionManager is a controller façade to the Spring Web Flow system. On receiving an event, it loads the flow defined by the '_flowId' parameter using Spring's bean factory (using XMLFlowFactoryBean). Then, it looks for an event parameter '_flowExecutionId' and if it isn't present, it will create a new FlowExecution instance and uses it to start this new flow. A FlowExecution encapsulates the execution state of a flow. Otherwise, the flow execution manager loads the existing flow execution instance from its storage (in our case this is HTTP Session-based storage) and uses it to continue execution. Figure 4 depicts this as a UML sequence diagram.
[Spring provides standard controllers to integrate with Spring MVC and struts framework. The controller servlet shown in Figure 4 and Figure 5 shows the logic that goes into these controllers.]
Let's look at each state in our sample checkout flow.
The 'viewShoppingCart' is a view state (it's also the start state). When the flow enters a view state, it triggers the rendering of a view. The view name defined by the 'view' attribute of any view state is a logical name that needs to be mapped to a physical view. In Spring MVC a ViewResolver and in struts an ActionForward can do this mapping. When the flow execution enters the 'viewShoppingCart' state, the flow execution suspends the active flow and returns a ViewDescriptor that represents a logical view 'sc.view.' A controller servlet can now map this logical view to a JSP (or any other view type) and forward the request to that JSP to render the view.
Once the user selects 'next' on the view shopping cart page, the controller servlet uses the flow execution manager to resume the flow execution. The flow execution manager requires the following parameters to continue flow execution:
- flowExecutionId - denotes the current flow execution state. [Required]
- currentStateId - denotes the current state. [Optional - required only to support browser back button usage]
- eventId - denotes the generated event. [Required]
//Lookup flow execution manager from spring's context
FlowExecutionManager mgr = ...
Map params = new HashMap();
// copy all request parameters to params HashMap
Event event= new Event(source, eventId, params);
ViewDescriptor vd = mgr.onEvent(event);
...
Figure 5 depicts this as a sequence diagram.
The flow execution manager loads the flow execution from the storage and uses it to process the event. The flow execution instance will apply the event on the known current state (the supplied current state will override this), which will result in a transition to a new state.
In our case, the current state is 'viewShoppingCart' and the '_eventId' value is 'next.' Since there's a transition defined for this criterion, it's executed and the flow execution enters the target state 'viewShippingAddress.' This will display the shipping address page to the user. Had the '_eventId' value been 'cancel' it would have resulted in entering the 'cancelFlow' state.
If the user now inputs the shipping details and selects 'next,' it should take the flow to the 'bindAndValidateSA' state, which is an action state. An action state executes one or more implementations of the 'Action' interface when entered. A typical action can act as a bridge between the presentation tier and the business tier or even execute the business logic directly (not a good practice).
The SWF supplies a ready-made 'action' implementation 'FormAction' for binding form parameters to a POJO bean. The 'FormAction' class extends from 'MultiAction,' which gives it the ability to dispatch a given method at runtime. The method to be executed can be specified using a 'method' attribute to an 'action' in the Web flow configuration.
In our example, we haven't specified a 'method' attribute and hence the default behavior is to execute a method with the same name as the state id, which is 'bindAndValidate.' The default implementation of the method 'bindAndValidate' in 'FormAction' dynamically binds the event parameter values to a POJO form bean, validates the bean if a 'validator' is specified, and finally places the form bean in the specified scope (flow scope or request scope).
In the checkout flow definition, the 'bindAndValidateSA' state has an action configured with a bean property of 'checkout.shippingAddress.' This is resolved using Spring's context XML.
<bean id="checkout.shippingAddress"
class="org.springframework.web.flow.action.FormAction">
<property name="formObjectName" value="shippingAddress"/>
<property name="formObjectClass" value="checkout.beans.ShippingAddress" />
< property name="formObjectScopeAsString" value="flow" />
</bean>
This form action uses the POJO 'checkout.beans.shippingAddress' and puts it in flow scope after binding the event parameter values. When this action completes execution, it signals a 'success' event. This 'success' event selects the transition that takes the flow to the 'viewBillingAddress' state. The flow then continues to execute this new state and so on.
When the flow enters an end state, it terminates the flow and displays the configured view.
<end-state id="finishCheckout" view="homePage" />
Sub Flows
Spring Web Flow also supports sub-flows. A sub-flow is a logically separate but complete flow in itself. It's similar to a subroutine in a procedural language. SWF has a separate state to denote a sub-flow. A sub-flow state can also refer to a flow defined in another flow definition file.
<subflow-state id="lookupPreviousShippingDetails" flow="shippingDetailsLookupFlow">
<attribute-mapper >
<input name="customerId"/>
<output name="shippingAddress">
</attribute-mapper>
<transition on = "success" to = "viewShippingAddress">
</subflow-state>
Published December 4, 2005 Reads 45,790
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Kishore Kumar
Kishore Kumar works as a Java architect at U.S. Technology (www.ustri.com). He specializes in J2EE applications.
![]() |
Eelco Hillenius 08/02/06 11:58:26 AM EDT | |||
'Page navigation' is a knee-jerk reaction that doesn't solve any real problems. See http://chillenious.wordpress.com/2006/07/16/on-page-navigation/ |
||||
![]() |
JD 08/01/06 06:53:29 PM EDT | |||
This article is really good. Two things missing in this article. The link or references to the actual JSPs and the link to the whole zipped source so people could run it and see for themselves. |
||||
![]() |
news desk 12/04/05 04:18:34 AM EST | |||
Java & SOA - The Promise of Handling Complex Page Navigations in Any Web Application. Page navigation requirements become more demanding as Web applications get bigger and more complex. Hard-coded page flow rules make applications less resilient to changes. In this scenario, reusing business logic is one aspect and reusing page flow becomes another aspect. Especially in situations that demand a wizard-kind behavior, it's essential to capture application page flow logic in a declarative fashion. |
||||
![]() |
jdj news desk 12/04/05 03:29:55 AM EST | |||
The Promise of Handling Complex Page Navigations in Any Web Application. Page navigation requirements become more demanding as Web applications get bigger and more complex. Hard-coded page flow rules make applications less resilient to changes. In this scenario, reusing business logic is one aspect and reusing page flow becomes another aspect. Especially in situations that demand a wizard-kind behavior, it's essential to capture application page flow logic in a declarative fashion. |
||||
![]() |
Jeanne 09/29/05 12:36:59 PM EDT | |||
Listings 2 and 3 are not linked in the article. |
||||
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?



































