0
Spring Web Flow:        A little flow of happiness.Сергей Моренец17 января 2013 г.
Agenda•   Overview of the navigation history•   Basics of SWF•   Practical usage•   Pros & Contras•   Q&A
Spring MVC
Navigation overview
JSP<jsp:forward page="/page.jsp" /><%response.sendRedirect(“page.jsp");%><form action="search.jsp" name="search" method=“p...
Spring MVCController@RequestMapping("/secure")public class NavigationController {     @RequestMapping("/operation")     pu...
Struts                     struts-config.xml<action-mappings>  <action path="/registration"type="net.sf.struts.flow.FlowAc...
JSF                      faces-config.xml<navigation-rule>  <from-view-id>page1.xhtml  </from-view-id>  <navigation-case> ...
Disadvantages•   Visualizing the flow is very difficult•   Mixed navigation and view•   Overall navigation rules complexit...
What is Spring Web Flow?•   Developed by Erwin Vervaet in 2004•   Initial version released in October, 2006•   Spring MVC ...
Erwin Vervaet• Belgium citizen• Holds masters degree in computer science• Originator of Spring Web Flow Project• Senior pr...
What is SWF for?• How do you express page navigation rules?• How do you manage navigation and conversational  state?• How ...
Use case
Request diagram
Flow definition• XML document with predefined elements• Flow definition is composed of a set of states• Each state has one...
Sample flow<flow xmlns="http://www.springframework.org/schema/webflow"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance...
Flow• A flow defines a conversion, or dialogue, between users  and server that meets business goal
State•   Action state•   View state•   Subflow state•   Decision state•   End state
Action state<action-state id="moreAnswersNeeded">  <evaluate expression="interview.moreAnswersNeeded()" />  <transition on...
View state<view-state id="answerQuestions" >      <on-entry>   <evaluate expression="interview.getNextQuestionSet()"result...
Decision state<decision-state id="moreAnswersNeeded">    <if test="interview.moreAnswersNeeded()"then="answerQuestions" el...
Subflow state<subflow-state id=“registerUser" subflow=“register">   <transition on=“userCreated" to=“finish">      <evalua...
End state<end-state id="finish" />
Start/end activity<on-start>  <evaluate expression=“mainService.startLock(currentUser)" /></on-start><on-end><evaluate exp...
Transition binding
JSF<h:form>              <h:commandButton action="save"value="#{msg[buttons.save]}" />              <h:commandButton actio...
Spring MVC<form:form>  <button type="submit" id=“save" name="_eventId_save">     <spring:message code=“buttons.save"/>  </...
Post redirect get(PRG)
Variables scope
Spring•   Singleton•   Prototype•   Request•   Session•   Global-session
SWF scope•   Request•   Flash•   View•   Flow•   Conversation
Request scope• Tied at the level of a single request• Not linked to a flow execution by itself
Flash scope• Extended request scope for PRG case• Useful for rendering error/warning messages
View scope• Can be referenced only within view state• Useful for storing data required with given view only
Flow scope• Lives within flow session• Not available inside sub-flows
Conversation scope• Lives within entire flow execution• Available to all sub-flows
Flow scope usage<var name="items" class="java.util.ArrayList" /><action-state id="init">  <on-entry>    <evaluateexpressio...
View scope usage<view-state id="viewitems">   <var name="items" class="java.util.ArrayList" />   <on-entry>     <evaluatee...
Presentation level<c:forEach items="#{items}“ var=“item">      <h:outputText value="#{item.text}" /></c:forEach><h:outputT...
Flow trackingOriginal link http://mysite/site/main?id=1Target http://mysite.com/site/main?execution=e2s1
Flow trackingpublic class PersistentUrlFlowHandler extendsDefaultFlowUrlHandler {       public String createFlowExecutionU...
Flow trackingOriginal link http://mysite/site/main?id=1Targethttp://mysite.com/site/main?execution=e2s1&id=1
Exception handling• Transition on exception• Custom exception handler
Transition on exception<view-state id="authenticate" view="login" >     <transition on="previous" to="previous"/>     <tra...
Exception handler<exception-handler bean="exceptionHandlerBean" /><bean id="exceptionHandlerBean"  class="org.bookstore.ex...
Exception handlerpublic class WebflowExceptionHandlerBean implementsFlowExecutionExceptionHandler {          private Strin...
Flow inheritance                           Parent-flow<flow … abstract="true">  <view-state id="someViewState" >     <tran...
Integration•   Spring MVC/Spring Security•   JSF 2•   Portlets•   RichFaces/Apache MyFaces•   Struts 2
Pros• High-level navigation control with clear observable  lifecycle• Designed to be self contained• Compile-independent• ...
Cons•   Requires Spring framework•   Separate Spring project•   Additional performance overhead•   Lack of community suppo...
Spring Tools Suite• Eclipse development environment for building  Spring-powered enterprise applications• Visualization of...
Flow graph
Flow editor
References• Spring Web Flow 2 Web Development. Markus  Stäuble, Sven Lüppken• Pro Spring MVC: With Web Flow. Marten  Deinu...
Q&A• Сергей Моренец, morenets@mail.ru
Spring Web Flow. A little flow of happiness.
Upcoming SlideShare
Loading in...5
×

Spring Web Flow. A little flow of happiness.

10,935

Published on

Spring Web Flow. A little flow of happiness - Sergey Morenets, senior developer, Provectus IT

1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,935
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
124
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Transcript of "Spring Web Flow. A little flow of happiness."

  1. 1. Spring Web Flow: A little flow of happiness.Сергей Моренец17 января 2013 г.
  2. 2. Agenda• Overview of the navigation history• Basics of SWF• Practical usage• Pros & Contras• Q&A
  3. 3. Spring MVC
  4. 4. Navigation overview
  5. 5. JSP<jsp:forward page="/page.jsp" /><%response.sendRedirect(“page.jsp");%><form action="search.jsp" name="search" method=“post"><input type="text" name="search" value="by keyword"><input type="submit" value="Go"></form>
  6. 6. Spring MVCController@RequestMapping("/secure")public class NavigationController { @RequestMapping("/operation") public String processOperationPage() { return “/corpus/operation"; }}<a href="secure/operation.htm">Operation</a>
  7. 7. Struts struts-config.xml<action-mappings> <action path="/registration"type="net.sf.struts.flow.FlowAction"className="net.sf.struts.flow.FlowMapping"> <forward name="name" path="/name.jsp"/> <forward name="hobbies" path="/hobbies.jsp"/> <forward name="summary" path="/summary.jsp"/> </action></action-mappings>
  8. 8. JSF faces-config.xml<navigation-rule> <from-view-id>page1.xhtml </from-view-id> <navigation-case> <from-outcome>page2</from-outcome> <to-view-id>/page2.xhtml</to-view-id> </navigation-case></navigation-rule><h:commandButton action="page2" value=“Next" />
  9. 9. Disadvantages• Visualizing the flow is very difficult• Mixed navigation and view• Overall navigation rules complexity• All-in-one navigation storage• Lack of state control/navigation customization
  10. 10. What is Spring Web Flow?• Developed by Erwin Vervaet in 2004• Initial version released in October, 2006• Spring MVC extension• Introduces flows concept• Extends application scopes• SWF 2.3.1 released in April, 2012
  11. 11. Erwin Vervaet• Belgium citizen• Holds masters degree in computer science• Originator of Spring Web Flow Project• Senior project manager together with Keith Donald• Speaker on the most Java and Spring related themes• Independent consultant www.ervacon.com
  12. 12. What is SWF for?• How do you express page navigation rules?• How do you manage navigation and conversational state?• How do you facilitate modularization and reuse?
  13. 13. Use case
  14. 14. Request diagram
  15. 15. Flow definition• XML document with predefined elements• Flow definition 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
  16. 16. Sample flow<flow xmlns="http://www.springframework.org/schema/webflow"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/webflowhttp://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">[variables][input parameters][states]</flow>
  17. 17. Flow• A flow defines a conversion, or dialogue, between users and server that meets business goal
  18. 18. State• Action state• View state• Subflow state• Decision state• End state
  19. 19. Action state<action-state id="moreAnswersNeeded"> <evaluate expression="interview.moreAnswersNeeded()" /> <transition on="yes" to="answerQuestions" /> <transition on="no" to="finish" /></action-state>public boolean moreAnswersNeeded() { return currentAnswer < answers.size();}
  20. 20. View state<view-state id="answerQuestions" > <on-entry> <evaluate expression="interview.getNextQuestionSet()"result="viewScope” </on-entry> <transition on="submitAnswers" to="moreAnswersNeeded"> <evaluate expression="interview.recordAnswers(questionSet)" /> </transition></view-state>
  21. 21. Decision state<decision-state id="moreAnswersNeeded"> <if test="interview.moreAnswersNeeded()"then="answerQuestions" else="finish" /></decision-state>public boolean moreAnswersNeeded() { return currentAnswer < answers.size();}
  22. 22. Subflow state<subflow-state id=“registerUser" subflow=“register"> <transition on=“userCreated" to=“finish"> <evaluate expression=“interview.addUser(currentEvent.attributes.user)“</transition></subfow-state>
  23. 23. End state<end-state id="finish" />
  24. 24. Start/end activity<on-start> <evaluate expression=“mainService.startLock(currentUser)" /></on-start><on-end><evaluate expression=“mainService.releaseLock(currentUser)" /></on-end>
  25. 25. Transition binding
  26. 26. JSF<h:form> <h:commandButton action="save"value="#{msg[buttons.save]}" /> <h:commandButton action="cancel"value="#{msg[buttons.cancel]}" immediate="true" /></h:form>
  27. 27. Spring MVC<form:form> <button type="submit" id=“save" name="_eventId_save"> <spring:message code=“buttons.save"/> </button> <button type="submit" id="cancel" name="_eventId_cancel"> <spring:message code="buttons.cancel"/> </button></form:form>
  28. 28. Post redirect get(PRG)
  29. 29. Variables scope
  30. 30. Spring• Singleton• Prototype• Request• Session• Global-session
  31. 31. SWF scope• Request• Flash• View• Flow• Conversation
  32. 32. Request scope• Tied at the level of a single request• Not linked to a flow execution by itself
  33. 33. Flash scope• Extended request scope for PRG case• Useful for rendering error/warning messages
  34. 34. View scope• Can be referenced only within view state• Useful for storing data required with given view only
  35. 35. Flow scope• Lives within flow session• Not available inside sub-flows
  36. 36. Conversation scope• Lives within entire flow execution• Available to all sub-flows
  37. 37. Flow scope usage<var name="items" class="java.util.ArrayList" /><action-state id="init"> <on-entry> <evaluateexpression="mainService.lookupItems(items)" /> </on-entry> <evaluate expression="items.size()"result="flowScope.size" /><transition on="success" to="viewitems" /></action-state>
  38. 38. View scope usage<view-state id="viewitems"> <var name="items" class="java.util.ArrayList" /> <on-entry> <evaluateexpression="mainService.lookupItems(items)" /> <evaluate expression="items.size()“ result="viewScope.size" /> </on-entry></view-state>
  39. 39. Presentation level<c:forEach items="#{items}“ var=“item"> <h:outputText value="#{item.text}" /></c:forEach><h:outputText value="#{size}" />
  40. 40. Flow trackingOriginal link http://mysite/site/main?id=1Target http://mysite.com/site/main?execution=e2s1
  41. 41. Flow trackingpublic class PersistentUrlFlowHandler extendsDefaultFlowUrlHandler { public String createFlowExecutionUrl(String flowId, StringflowExecutionKey, HttpServletRequest request) { String url =super.createFlowExecutionUrl(flowId, flowExecutionKey, request); if (request.getParameter("id") != null) { StringBuilder builder = new StringBuilder(url); builder.append("&"); appendQueryParameter(builder, "id", request.getParameter("id")); return builder.toString(); } return url; }
  42. 42. Flow trackingOriginal link http://mysite/site/main?id=1Targethttp://mysite.com/site/main?execution=e2s1&id=1
  43. 43. Exception handling• Transition on exception• Custom exception handler
  44. 44. Transition on exception<view-state id="authenticate" view="login" > <transition on="previous" to="previous"/> <transition on-exception="com.bookstore. AuthenticationException" to=“error_handler"/></view-state><action-state id=“error_handler”> ….</action-state>
  45. 45. Exception handler<exception-handler bean="exceptionHandlerBean" /><bean id="exceptionHandlerBean" class="org.bookstore.exception. ExceptionHandlerBean"> <property name="errorUrl" value="/error“ /></bean>
  46. 46. Exception handlerpublic class WebflowExceptionHandlerBean implementsFlowExecutionExceptionHandler { private String errorUrl; public boolean canHandle(FlowExecutionException ex) { return findServiceException(ex) != null; } public void handle(FlowExecutionException ex, RequestControlContext context) { Exception flowException = findServiceException(ex); context.getExternalContext().requestExternalRedirect(errorUrl);} private Exception findServiceException(FlowExecutionException ex) { Throwable cause = ex.getCause(); if (cause instanceof AuthenticationException) { return (Exception)cause; } return null;}}
  47. 47. Flow inheritance Parent-flow<flow … abstract="true"> <view-state id="someViewState" > <transition on="x" to="someState"/> </view-state></flow> Child-flow<flow parent="parent-flow"> <view-state id="someViewState" > <transition on="y" to="someOtherState"/> </view-state></flow>
  48. 48. Integration• Spring MVC/Spring Security• JSF 2• Portlets• RichFaces/Apache MyFaces• Struts 2
  49. 49. Pros• High-level navigation control with clear observable lifecycle• Designed to be self contained• Compile-independent• Easy to understand and visualize• Expression language support• Custom validation (including AJAX)• Integrates with major web frameworks
  50. 50. Cons• Requires Spring framework• Separate Spring project• Additional performance overhead• Lack of community support• Not suitable for simple or flow-free applications
  51. 51. Spring Tools Suite• Eclipse development environment for building Spring-powered enterprise applications• Visualization of Spring Web Flow
  52. 52. Flow graph
  53. 53. Flow editor
  54. 54. References• Spring Web Flow 2 Web Development. Markus Stäuble, Sven Lüppken• Pro Spring MVC: With Web Flow. Marten Deinum, Koen Serneels, Colin Yates, Seth Ladd, Christophe Vanfleteren• Spring in Action.3rd edition .Craig Walls
  55. 55. Q&A• Сергей Моренец, morenets@mail.ru
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×