Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Web Flow An Overview of Spring’s Event Based Web Application Framework
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4 What Is the Problem? Take a simple use case –A wizard for a phone operator to use to sell items to customers Characteristics: –An “application transaction” that spans several steps –Some steps solicit user input –Some steps are decision points –Some steps perform calculations –Navigation from step-to-step is controlled
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5 How Would You Solve This With Struts? 1.Create a session-scoped ActionForm to hold the wizard form data 2.Define a JSP for each step 3.Define an Action for each step 4.Expose each Action at a request URL 5.Have the form rendered by the JSP submit to that URL 6.At the end, delegate to a business service to commit the transaction
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6 What this looks like /step2 Controller2 page2 partial data HTTP Session /step1 Controller1 partial data page1 /lastStep Controller3 confirmationPage processTx partial data Business Service
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7 Issues with this approach Request centric: no concept of an ongoing conversation or flow Brittle dependency on request URLs Manual state management Odd new window behavior Proper back button behavior is difficult “Loose” controlled navigation Difficult to observe process lifecycle Controller and command logic are coupled Heavily tied to HTTP
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9 Solving the Right Problem The fundamental problem is that we have been working at the wrong level While HTTP is stateless, our business processes are not The web applications we write are for supporting business processes, not HTTP We need a way to work at the process level and let the computer handle the details of how to do that on the web
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10 How Would You Solve This With SWF? 1.Define a flow that implements your business process 2.Define a web page for each place user input is needed (ie, each “view state”)
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11 What this looks like in SWF /theflow event view 2 Business Service /theflow event last view processTx /theflow view 1 The flow
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12 What Is a Flow? A Flow definition serves as instructions to a finite state machine It consists of a set of states that you define Each state executes a polymorphic behavior when entered –View states solicit user input –Action states execute commands –Subflow states spawn child flows –End states terminate flows Events you define drive state transitions
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13 A “Sell Item” Flow Definition start Enter Price and Item Count submit Select Item Category Is Shipping Required? Enter Shipping Information yes no Process Sale submit Show Cost Overview View State Action State Decision State End State
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15 Some Big Advantages Emerge Navigation enforcement Automatic state management Modularity & Decoupling Abstraction Toolability Facilites Agile Development Cycle Flow lifecycle visibility
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16 Navigation Enforcement Web Flow ensures defined navigation rules can not be bypassed or short-circuited –This happens for free –Without losing any power compared to other approaches
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17 Automatic State Management Flow scope state is managed for you automatically –Conceptually no different than existing scopes (e.g. Session, Request), but focussed on the process rather than the web –Put something in flow scope It’s there while the flow lives It’s cleaned up after the flow is done
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18 Modularity & Decoupling Navigation rules are encapsulated in one place –Controller logic is clearly decoupled from command logic –The Flow is the controller, deciding what state to enter next –States execute arbitrary behavior when entered The logical flow is clearly defined, readable Flows are Modules the same way a class is a module
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19 Toolability Flow definitions are extremely toolable Well suited to graphical editing Well suited to programmatic generation Spring IDE 2.0 knows about Web Flow –Web Flow Graphical Editor –Web Flow XML Editor –Use for visualization: a communication tool –Use for editing: easier for new developers
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20 Abstraction Flows are very similar in different environments (JSF, Spring MVC, Flex, portlets, etc.) –Could be completely identical for different environments –Or slight differences where Web Flow capabilities overlap with base Web UI Framework capabilities E.g. use Web flow validation, or JSF validation?
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21 Facilitates Agile Development Cycle Flows are re-compiled automatically, on the fly –Test your changes without restarting servlet container
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23 How Does Webflow State Management Work? SWF stores the current status of the overall conversation in a repository Each transition in the flow also has its status stored in the repository
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24 Conversations & Continuations AB C C1C1 B C C1C1 C _k8D2_kA35_k7BC _c1B2 _kB2E _c1B2
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25 State Management AB C C1C1 B C C1C1 C _k8D2_kA35_k7BC _c1B2 _kB2E _c1B2 flow scope conversation scope
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26 State Storage Where should the data be physically stored? –To enable zero resource usage on the server? –To allow a process to be active for a long time (eg, so the person can resume in the morning) –For typical flows where the user is actively interacting with the application? –To allow sending a URL to an admin to finish booking your travel plans? Persistence is a separate concern easily swapped out depending on needs
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27 States – 5 Main types View State –Pause and allow the user to participate in a flow Action State –Execute business logic, usually indirectly Decision State –Make a flow routing decision Subflow State –Spawn a sub flow End State –Terminate a flow
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28 Getting Rid of “Double Submit” Why do browsers ask if you would like to re-post the data when you go back to a page that was created as the result of a post? Spring Web Flow follows the web “best practice” of using GET for reads and POST for writes. Why do you not see “repost” dialogs when you move back in a SWF application? SWF does “Post+Redirect+Get”, so no page is ever rendered as the result of a POST.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29 Subflows Flows are modular components, and can be “composed” Some processes will naturally break into subflows. –Allows for reuse of flows –Can make it much easier to manage complex flows
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 30 Topics in this Session What Is The Problem? Solving the Right Problem Advantages of the New Focus State and Flow Management Design Tips
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 31 What makes a good Web Flow? –Accomplishes some business goal, e.g. Book a trip Pay your taxes Apply for a Loan –Encapsulates that business goal as a reusable module –Entry point to business goal is a public URL, but intermediate steps are valid only in the context of the flow (i.e. may not be bookmarked) –Often has multiple paths to support different scenarios Paths may vary based on contextual information –Often spawns other Flows as sub flows Design Tips – Good For Flows
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 32 What doesn’t make a good Flow? –Any set of pages with free-form navigation between them Publicly accessible URLs Often able to be bookmarked Often backed by individual controller or controller chain –E.g. Index pages, welcome pages, item detail pages, status pages, menus Web flows are best suited for enforcing controlled navigation to support business process workflows –As a compliment to traditional controllers Design Tips – Bad For Flows