920 likes | 1.95k Views
Spring Web Flow. Introduction. Spring Web Flow is a subproject of the popular Spring Framework. http://www.springframework.org/webflow. Introduction.
E N D
Introduction • Spring Web Flow is a subproject of the popular Spring Framework http://www.springframework.org/webflow
Introduction • Spring Web Flow allows to build high-level, reusable, self-contained controller modules called flows that are runnable in any environment
Flows • A flow defines a user dialog that responds to user events to drive the execution of application code to complete a business goal • Flows are defined declaratively using a rich domain-specific language, like XML (XSD)
Motivation [part1 - flow definition] • Traditionally, defining the UI flow in a web application has been a less than intuitive process • Frameworks like Spring Web MVC force to cut the UI flow into individual controllers and views • Disadvantage: the overall UI flow is not at all clear from looking at the controller definitions
Motivation [part1 - flow definition] • The Spring Web MVC offers a slightly higher level of functionality - form controllers that implement a predefined work flow: • SimpleFormController • AbstractWizardFormController • However, these are still hard coded examples of a more general work flow concept Spring Web Flow
Spring Web Flow solution • Allows to represent the UI flow in a web application in a clear and simple way • The UI flow is clearly visible by looking at the web flow definition (XML file) • Web Flows can be designed to be self-contained modular design and reuse • Consistent web flow definition technique you're not forced into using specialized controllers for very particular situations
Motivation [part2 - navigation] • Typical web application have a mix of navigation requirements • Free navigations • Controlled flows • Each case bring their own design challenges
Free navigation • A set of pages connected by links • Accessing a link renders a public resource • Examples • Kursa lekciju saraksts: • http://www.webkursi.lv/java-eim/de/index_lectures_lu.html • Otrais praktiskais darbs: • http://www.webkursi.lv/java-eim/de/lab_mvc_portal.html • Users can access resources directly • Can bookmark or send to a friend
Controlled flows • A user task that spans more than one request • The task is the public resource • Examples • Registration • E-shop ordering • Accessing the task URL starts a new “task execution” • Local to the current user’s session • Intermediate steps of an execution are not bookmarked
Effect on design • Free navigation • Stateless • When invoked: • Does work • Selects a view to render • All in one request • A controlled flow is more complex • Stateful • Guides the user through a task with a linear progression • Renders a view to solicit task input as required
Motivation [part3 - scopes] • Three different Servlet scopes that dictate the visibility and location of objects and attributes that are associated with request processing: • Request • Session • Application
Request scope • Has the smallest lifetime • Is unique to each request from a browser • Will be discarded when the view is returned to the browser • Typically contains data sent from the browser • browser headings • request parameters
Session scope • Starts as soon as each unique browser accesses the server • Ends if the server hasn’t received a request within a specified timeout or if explicitly destroyed • This scope allows objects to live across requests • Typically contains e.g. information about the current user • user details • authentication
Application scope • Lives for the duration of the web application deployment • Contains shared application components and configuration elements
The problem with scopes • Many use cases do not fit into the Servlet scopes • Use case span more than one page but do not require the longevity of the session • The Servlet specification is missing the concept of a conversational scope to support the execution of use cases that span multiple pages
Session scope? Why not store everything in the session and manually perform cleanups when the conversation ends? • Server affinity • Greatly increased memory footprint per user • Name space clashes • No vocabulary for process modelling
Spring solution • Spring Web Flow treats conversational scope as a first-level citizen • The core artefact within Spring Web Flow is the flow (or conversation) • Conversations can execute in parallel without intruding on each other • When the conversation has finished, all allocated resources are automatically cleaned up
Spring Web Flow characteristics • Implementation agnostic • Abstracted away from the Servlet specification • Nothing web-specific about a flow definition • Developer is not presented with an HttpServletRequest or an HttpServletResponse
Spring best practices • Develop against interfaces plug in the most appropriate implementation • Favor integration with established technologies • Allow reusability from within other established web frameworks • Integrates with Struts, JSF and Portlet MVC • Facilitate test-driven development
Spring Web Flow characteristics • Low adoption overhead • SWF is self-contained little impact when introducing it on existing projects • Spring Web Flow is another Controller tool in the MVC toolbox • A major design goal of Spring Web Flow was to do one thing and do it well
Integration options • Spring Web Flow is very self-contained the entry points to other frameworks are consistent
Inside the SWF System • A central FlowExecutionManager façade is responsible for launching executions of flows on behalf of clients • Flow executions = new user conversations with the server • Each conversation is given its own local data structure, called flow scope
Where is flow scope stored? • Flow scope is stored in a repository • A number of repository implementations to support different usage scenarios: • SimpleFlowExecutionRepository • ContinuationFlowExecutionRepository • ClientContinuationFlowExecutionRepository http://en.wikipedia.org/wiki/Continuation
Building Blocks • With Spring Web Flow, the primary challenge for developers is the design and implementation of aflow definition • Web flow is composed of a set of states • Each state has one or more transitions that are used to move to another state • A transition is triggered by an event
Flows • A flow defines a conversation, or dialogue, between users and theserver
States The steps of a flow are called states. Fivecore types of states: • Action state • Executes application code, typically delegating to a business service in themiddle tier • View state • Renders a view allowing the user to participate in the flow by entering data orviewing a message
States • Subflow state • Spawns another flow as a subflow • Decision state • Evaluates a condition to drive a transition to a new state • End state • Terminates a flow
Transitions • All states (except end states) are transtionable and maintain a set of one or more transitions thatdefine “allowed paths” to other states • A transition is triggered on the occurrence of an event
Events • Nothing more than “something that happens” within a state • An event is treated as a state outcome that captures the logical result of a state’s execution • From the previous slide: • The “submit” event communicates that a submit button was pressed as the outcome of a view state • The “yes” event communicates that a “true” result was returned when evaluating a condition as the outcome of a decision state
Deterministic finite automata http://www.u.arizona.edu/~miller/webthesis/mthesis/node8.html
Sample Application - Phonebook • Allows to locate an employee of the company using some search criteria • Once the right person is found, it is possible to consult detailed information (phone, colleagues) http://www.ervacon.com/products/swf/intro/index.html
Sources and deployed sample Sources: \spring-webflow-1.0.5\projects\spring-webflow-samples\phonebook Deployed sample: http://spring.ervacon.com/swf-phonebook/
Phonebook domain objects • Person - A simple JavaBean containing person details • SearchCriteria - A query object representing a search in the phonebook • SearchCriteriaValidator - A validator to validate a SearchCriteria object • Phonebook - Main business facade interface • public List<Person> search(SearchCriteriacriteria); • public Person getPerson(Long id); • The StubPhonebook implementation of the Phonebook interface just hard-codes some dummy data
Spring Web MVC Setup Need to configure /WEB-INF/web.xml • ContextLoaderListener - initializes the Spring Framework when web application is loaded by the servlet engine • DispatcherServlet - handles all requests matching the URL pattern *.htm
ContextLoaderListener <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:org/springframework/webflow/samples/phonebook/stub/services-config.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> The root application context will be loaded from the services-config.xml classpath resource
services-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="phonebook" class="org.springframework.webflow. samples.phonebook.stub.StubPhonebook"/> </beans> The root web application context, defined in services-config.xml configures business facade: the phonebook bean
DispatcherServlet <servlet> <servlet-name>phonebook</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/phonebook-servlet-config.xml /WEB-INF/phonebook-webflow-config.xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>phonebook</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>
ViewResolver <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet. view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
Spring Web MVC is configured! Next let’s integrate Spring Web Flow!
The Web Flow Controller • The component that does integration is: org.springframework.webflow.executor.mvc.FlowController • This controller will handle all things Web Flow on behalf of the application • Configure in phonebook-servlet-config.xml: <bean name="/phonebook.htm" class= "org.springframework.webflow.executor.mvc.FlowController"> <property name="flowExecutor" ref="flowExecutor"/> </bean>
Flow executor • All flow execution management responsibilities will be delegated to flow executor • phonebook-webflow-config.xml: <?xml version="1.0" encoding="UTF-8"?> <beans ... > <flow:executor id="flowExecutor" registry-ref="flowRegistry"/> <flow:registry id="flowRegistry"> <flow:location path="/WEB-INF/flows/**-flow.xml"/> </flow:registry> </beans>
Flow registry • The flow registry will load flows from XML files found in the /WEB-INF/flows/ directory that have the -flow.xml suffix • The search flow defined in search-flow.xml will be assigned the id search-flow • The detail flow defined in detail-flow.xml will receive the id detail-flow
Request parameters • Web Flow controller is invoked by the dispatcher servlet to handle a request • It examines the following request parameters to determine what to do: • _flowId • _flowExecutionKey • _eventId
_flowId • Id of a flow for which a new execution should be launched • Possible values in a sample application are • search-flowand detail-flow • If "_flowExecutionKey" parameter is not present, the flow controller launches a new flow execution • The new execution will be assigned a unique key <a href="phonebook.htm?_flowId=search-flow">Phonebook</a>