56
Spring Web Flow: A little flow of happiness. Сергей Моренец 17 января 2013 г.

Spring Web Flow. A little flow of happiness

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Spring Web Flow. A little flow of happiness

Spring Web Flow:A little flow of happiness.

Сергей Моренец17 января 2013 г.

Page 2: Spring Web Flow. A little flow of happiness

Agenda

• Overview of the navigation history• Basics of SWF• Practical usage• Pros & Contras• Q & A

Page 3: Spring Web Flow. A little flow of happiness
Page 4: Spring Web Flow. A little flow of happiness

Spring MVC

Page 5: Spring Web Flow. A little flow of happiness

Navigation overview

Page 6: Spring Web Flow. A little flow of happiness

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>

Page 7: Spring Web Flow. A little flow of happiness

Spring MVCController @RequestMapping("/secure") public class NavigationController {

@RequestMapping("/operation") public String processOperationPage() { return “/corpus/operation"; } }

<a href="secure/operation.htm">Operation</a>

Page 8: Spring Web Flow. A little flow of happiness

Strutsstruts-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>

Page 9: Spring Web Flow. A little flow of happiness

JSFfaces-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" />

Page 10: Spring Web Flow. A little flow of happiness

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

Page 11: Spring Web Flow. A little flow of happiness

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

Page 12: Spring Web Flow. A little flow of happiness

Erwin Vervaet• Belgium citizen• Holds master's 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

Page 13: Spring Web Flow. A little flow of happiness

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?

Page 14: Spring Web Flow. A little flow of happiness

Use case

Page 15: Spring Web Flow. A little flow of happiness

Request diagram

Page 16: Spring Web Flow. A little flow of happiness

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

Page 17: Spring Web Flow. A little flow of happiness

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>

Page 18: Spring Web Flow. A little flow of happiness

Flow• A flow defines a conversion, or dialogue, between

users and server that meets business goal

Page 19: Spring Web Flow. A little flow of happiness

State• Action state• View state• Subflow state• Decision state• End state

Page 20: Spring Web Flow. A little flow of happiness

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();}

Page 21: Spring Web Flow. A little flow of happiness

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>

Page 22: Spring Web Flow. A little flow of happiness

Decision state<decision-state id="moreAnswersNeeded"> <if test="interview.moreAnswersNeeded()" then="answerQuestions" else="finish" /></decision-state>

public boolean moreAnswersNeeded() { return currentAnswer < answers.size();}

Page 23: Spring Web Flow. A little flow of happiness

Subflow state<subflow-state id=“registerUser" subflow=“register"> <transition on=“userCreated" to=“finish"> <evaluate expression=“interview.addUser( currentEvent.attributes.user)“</transition></subfow-state>

Page 24: Spring Web Flow. A little flow of happiness

End state

<end-state id="finish" />

Page 25: Spring Web Flow. A little flow of happiness

Start/end activity<on-start> <evaluate expression=“mainService.startLock(currentUser)" /></on-start>

<on-end> <evaluate expression=“mainService.releaseLock(currentUser)" /></on-end>

Page 26: Spring Web Flow. A little flow of happiness

Transition binding

Page 27: Spring Web Flow. A little flow of happiness

JSF<h:form>

<h:commandButton action="save" value="#{msg['buttons.save']}" />

<h:commandButton action="cancel" value="#{msg['buttons.cancel']}"

immediate="true" /></h:form>

Page 28: Spring Web Flow. A little flow of happiness

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>

Page 29: Spring Web Flow. A little flow of happiness

Post redirect get(PRG)

Page 30: Spring Web Flow. A little flow of happiness

Variables scope

Page 31: Spring Web Flow. A little flow of happiness

Spring• Singleton• Prototype• Request• Session• Global-session

Page 32: Spring Web Flow. A little flow of happiness

SWF scope• Request• Flash• View• Flow• Conversation

Page 33: Spring Web Flow. A little flow of happiness

Request scope• Tied at the level of a single request• Not linked to a flow execution by itself

Page 34: Spring Web Flow. A little flow of happiness

Flash scope• Extended request scope for PRG case• Useful for rendering error/warning messages

Page 35: Spring Web Flow. A little flow of happiness

View scope• Can be referenced only within view state• Useful for storing data required with given view

only

Page 36: Spring Web Flow. A little flow of happiness

Flow scope• Lives within flow session• Not available inside sub-flows

Page 37: Spring Web Flow. A little flow of happiness

Conversation scope• Lives within entire flow execution• Available to all sub-flows

Page 38: Spring Web Flow. A little flow of happiness

Flow scope usage<var name="items" class="java.util.ArrayList" />

<action-state id="init"> <on-entry> <evaluate expression="mainService.lookupItems(items)" /> </on-entry> <evaluate expression="items.size()" result="flowScope.size" /><transition on="success" to="viewitems" /></action-state>

Page 39: Spring Web Flow. A little flow of happiness

View scope usage<view-state id="viewitems"> <var name="items" class="java.util.ArrayList" /> <on-entry> <evaluate expression="mainService.lookupItems(items)" /> <evaluate expression="items.size()“ result ="viewScope.size" /> </on-entry></view-state>

Page 40: Spring Web Flow. A little flow of happiness

Presentation level <c:forEach items="#{items}“ var=“item"> <h:outputText value="#{item.text}" /></c:forEach>

<h:outputText value="#{size}" />

Page 42: Spring Web Flow. A little flow of happiness

Flow trackingpublic class PersistentUrlFlowHandler extends DefaultFlowUrlHandler {

public String createFlowExecutionUrl(String flowId,String flowExecutionKey, 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;

}

Page 43: Spring Web Flow. A little flow of happiness

Flow trackingOriginal link http://mysite/site/main?id=1

Target http://mysite.com/site/main?execution=e2s1&id=1

Page 44: Spring Web Flow. A little flow of happiness

Exception handling• Transition on exception• Custom exception handler

Page 45: Spring Web Flow. A little flow of happiness

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>

Page 46: Spring Web Flow. A little flow of happiness

Exception handler<exception-handler bean="exceptionHandlerBean" />

<bean id="exceptionHandlerBean" class="org.bookstore.exception. ExceptionHandlerBean"> <property name="errorUrl" value="/error“ /></bean>

Page 47: Spring Web Flow. A little flow of happiness

Exception handlerpublic class WebflowExceptionHandlerBean implements FlowExecutionExceptionHandler {

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;

}}

Page 48: Spring Web Flow. A little flow of happiness

Flow inheritanceParent-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>

Page 49: Spring Web Flow. A little flow of happiness

Integration• Spring MVC/Spring Security• JSF 2• Portlets• RichFaces/Apache MyFaces• Struts 2

Page 50: Spring Web Flow. A little flow of happiness

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

Page 51: Spring Web Flow. A little flow of happiness

Cons• Requires Spring framework• Separate Spring project• Additional performance overhead• Lack of community support• Not suitable for simple or flow-free applications

Page 52: Spring Web Flow. A little flow of happiness

Spring Tools Suite

• Eclipse development environment for building Spring-powered enterprise applications

• Visualization of Spring Web Flow

Page 53: Spring Web Flow. A little flow of happiness

Flow graph

Page 54: Spring Web Flow. A little flow of happiness

Flow editor

Page 55: Spring Web Flow. A little flow of happiness

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

Page 56: Spring Web Flow. A little flow of happiness

Q&A

• Сергей Моренец, [email protected]