Top Banner
SPRING FRAMEWORK Spring Web Flow 2.3 Dmitry Noskov Spring Framework - Web Flow
58

Spring Framework - Web Flow

Nov 18, 2014

Download

Technology

Dmitry Noskov

Introduction to Spring Web Flow.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spring Framework - Web Flow

SPRING FRAMEWORK

Spring Web Flow 2.3 Dmitry Noskov

Spring Framework - Web Flow

Page 2: Spring Framework - Web Flow

The Spring WEB stack

Dmitry Noskov Spring Framework - Web Flow

Page 3: Spring Framework - Web Flow

Introduction

MVC extension

focused on the definition and execution of page

flow

Integration

Spring MVC

JSF

Portlet

Dmitry Noskov Spring Framework - Web Flow

Page 4: Spring Framework - Web Flow

What is flow?

Dmitry Noskov Spring Framework - Web Flow

Page 5: Spring Framework - Web Flow

Typical web application

Dmitry Noskov Spring Framework - Web Flow

Page 6: Spring Framework - Web Flow

Process driven application

Dmitry Noskov Spring Framework - Web Flow

Page 7: Spring Framework - Web Flow

Configuration

Dmitry Noskov Spring Framework - Web Flow

Page 8: Spring Framework - Web Flow

Web Flow schema

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:webflow="http://www.springframework.org/schema/webflow-config"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/webflow-config

http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">

Dmitry Noskov Spring Framework - Web Flow

Page 9: Spring Framework - Web Flow

Flow builder

basic <webflow:flow-builder-services

id="flowBuilderServices"

view-factory-creator="mvcViewFactoryCreator"

development="true"

validator="validator"

conversion-service="customConversionService"

expression-parser="ognl"/>

Dmitry Noskov Spring Framework - Web Flow

Page 10: Spring Framework - Web Flow

Flow registry

basic <webflow:flow-registry id="flowRegistry"

flow-builder-services="builderServices"

base-path="/WEB-INF"

parent="parent">

<!—flow locations-->

</webflow:flow-registry>

specify path <webflow:flow-location id=""

path="/hotels/booking/booking-flow.xml"/>

location pattern <webflow:flow-location-pattern value="/**/*-flow.xml" />

Dmitry Noskov Spring Framework - Web Flow

Page 11: Spring Framework - Web Flow

Flow executor

basic <webflow:flow-executor id="flowExecutor" flow-registry="ref">

<!—listeners, repositories-->

</webflow:flow-executor>

listener <webflow:flow-execution-listeners>

<webflow:listener ref="listener" criteria="*"/>

</webflow:flow-execution-listeners>

repository <webflow:flow-execution-repository max-executions="5"

max-execution-snapshots="10"/>

Dmitry Noskov Spring Framework - Web Flow

Page 12: Spring Framework - Web Flow

Workflow image

Dmitry Noskov Spring Framework - Web Flow

Page 13: Spring Framework - Web Flow

Dispatcher servlet

<servlet>

<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value></param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

Dmitry Noskov Spring Framework - Web Flow

Page 14: Spring Framework - Web Flow

Dispatching

FlowHandlerMapping <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">

<property name="order" value="-1"/>

<property name="flowRegistry" ref="flowRegistry" />

</bean>

FlowHandlerAdapter <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">

<property name="flowExecutor" ref="flowExecutor"/>

</bean>

Dmitry Noskov Spring Framework - Web Flow

Page 15: Spring Framework - Web Flow

Flow

Dmitry Noskov Spring Framework - Web Flow

Page 16: Spring Framework - Web Flow

Flow schema

<?xml version="1.0" encoding="UTF-8"?>

<flow xmlns="http://www.springframework.org/schema/webflow"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation=“

http://www.springframework.org/schema/webflow

http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

…states…

</flow>

Dmitry Noskov Spring Framework - Web Flow

Page 17: Spring Framework - Web Flow

Essential elements

state

transition

flow data

expression language

Dmitry Noskov Spring Framework - Web Flow

Page 18: Spring Framework - Web Flow

Input attribute

simple <input name="hotelId" required="true" />

assign input value <input name="hotelId" value="flowScope.bean.itemId" />

Dmitry Noskov Spring Framework - Web Flow

Page 19: Spring Framework - Web Flow

Output value

flow attribute <end-state id="bookingConfirmed">

<output name="booking"/>

</end-state>

specific <end-state id="bookingConfirmed">

<output name="booking" value="booking.id"/>

</end-state>

Dmitry Noskov Spring Framework - Web Flow

Page 20: Spring Framework - Web Flow

Data scope

Dmitry Noskov Spring Framework - Web Flow

Conversation

Flow

Request

Flash

View

Page 21: Spring Framework - Web Flow

States

view

action

decision

subflow

end

Dmitry Noskov Spring Framework - Web Flow

Page 22: Spring Framework - Web Flow

Expression language

expression types

standard

template

implementation

Spring EL

Unified EL

OGNL

Dmitry Noskov Spring Framework - Web Flow

Page 23: Spring Framework - Web Flow

Special variables

flowScope, viewScope, etc.

requestParameters

currentEvent

currentUser

messageContext, resourceBoundle

flowRequestContext, flowExecutionContext

flowExecutionUrl

externalContext

Dmitry Noskov Spring Framework - Web Flow

Page 24: Spring Framework - Web Flow

Typical flow

Dmitry Noskov Spring Framework - Web Flow

Page 25: Spring Framework - Web Flow

Rendering views

Dmitry Noskov Spring Framework - Web Flow

Page 26: Spring Framework - Web Flow

Simple view

view state <view-state id="enterBookingDetails" model="booking" view="path">

<transition on="proceed" to="reviewBooking" />

<transition on="cancel" to="cancel" />

</view-state>

view detection

by state id

by relative view id

by absolute view id

Dmitry Noskov Spring Framework - Web Flow

Page 27: Spring Framework - Web Flow

View resolution

default

mvc view resolver <webflow:flow-builder-services view-factory-creator="mvc" />

<bean id="mvc"

class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">

<property name="viewResolvers" ref="tilesViewResolver"/>

<property name="useSpringBeanBinding" value="true" />

</bean>

Dmitry Noskov Spring Framework - Web Flow

Page 28: Spring Framework - Web Flow

Firing events

simple link <a href="${flowExecutionUrl}&_eventId=cancel/">Cancel</a>

hidden field <input type="submit" value="Cancel" />

<input type="hidden" name="_eventId" value="cancel" />

submit button <button type="submit" name="_eventId_proceed">Proceed</button>

<button type="submit" name="_eventId_cancel">Cancel</button>

Dmitry Noskov Spring Framework - Web Flow

Page 29: Spring Framework - Web Flow

Advanced view

<view-state id="enterBookingDetails"

view=""

model="booking"

parent=""

popup="true/false"

redirect="true/false" >

<var name="" class=""/>

<binder>

<binding property="checkinDate" />

</binder>

</view-state>

Dmitry Noskov Spring Framework - Web Flow

Page 30: Spring Framework - Web Flow

Model binding

<on-start>

<evaluate expression="bookingService.createBooking(hotelId)"

result="flowScope.booking" />

</on-start>

<view-state id="enterBookingDetails" model="booking">

<binder>

<binding property="checkinDate" />

<binding property="checkoutDate" />

<binding property="creditCard" />

</binder>

<transition on="proceed" to="reviewBooking" />

<transition on="cancel" to="cancel" bind="false" />

</view-state>

Dmitry Noskov Spring Framework - Web Flow

Page 31: Spring Framework - Web Flow

Validation

JSR-303 <webflow:flow-builder-services id="" validator="validator" />

<bean id="validator" class="…LocalValidatorFactoryBean"/>

model validate method public class Booking implements Serializable {

public void validateEnterBookingDetails(ValidationContext c) {}

}

separate validator public class BookingValidator {

public void validate(Booking b, ValidationContext c) {}

public void validateEnterBookingDetails(…) {}

}

Dmitry Noskov Spring Framework - Web Flow

Page 32: Spring Framework - Web Flow

Execution actions

Dmitry Noskov Spring Framework - Web Flow

Page 33: Spring Framework - Web Flow

Actions

set variable <set name="flowScope.key" value="value"/>

evaluate business method <evaluate expression="service.find()" result="viewScope.key"/>

render view fragment <render fragments="body" />

Dmitry Noskov Spring Framework - Web Flow

Page 34: Spring Framework - Web Flow

Events

flow

on-start

on-end

state

on-entry

on-render (only for view)

on-exit

transition

Dmitry Noskov Spring Framework - Web Flow

Page 35: Spring Framework - Web Flow

Example

<flow>

<on-start>

<evaluate

expression="bookingService.createBooking(hotelId)"

result="flowScope.booking" />

</on-start>

<view-state id="enterBookingDetails" model="booking">

<on-render>

<render fragments="body" />

</on-render>

<transition on="proceed" to="reviewBooking" />

<transition on="cancel" to="cancel" bind="false" />

</view-state>

</flow>

Dmitry Noskov Spring Framework - Web Flow

Page 36: Spring Framework - Web Flow

Business actions

POJO <evaluate expression="service.businessMethod()" />

action public class CustomAction implements Action {

public Event execute(RequestContext context) {}

}

<evaluate expression="customAction" />

multi action

Dmitry Noskov Spring Framework - Web Flow

Page 37: Spring Framework - Web Flow

Action state

simple <action-state id="addBooking">

<evaluate expression="bookingService.addBooking()" />

<transition to="cancel"/>

</action-state>

transitioning on evaluation <action-state id="cancelBooking">

<evaluate expression="bookingService.cancelBooking()" />

<transition on="cancelled" to="endState"/>

<transition on="bookingNotFound" to="booking"/>

<transition on-exception="org.swf.AccessDenied" to="booking"/>

</action-state>

Dmitry Noskov Spring Framework - Web Flow

Page 38: Spring Framework - Web Flow

Event mapping

Return type Event identifier

java.lang.String the String value

java.lang.Boolean yes / no

java.lang.Enum the enum name

any other type success

Dmitry Noskov Spring Framework - Web Flow

Page 39: Spring Framework - Web Flow

Named actions

<action-state id="callTwoMethods">

<evaluate expression="service.methodOne()">

<attribute name="name" value="methodOne" />

</evaluate>

<evaluate expression="service.methodTwo()">

<attribute name="name" value="methodTwo" />

</evaluate>

<transition on="methodTwo.success" to="showResults" />

</action-state>

Dmitry Noskov Spring Framework - Web Flow

Page 40: Spring Framework - Web Flow

Decision

<decision-state id="hasItems">

<if test="service.hasItems()"

then="checkItemsState"

else="end" />

</decision-state>

Dmitry Noskov Spring Framework - Web Flow

Page 41: Spring Framework - Web Flow

Subflow

<subflow-state id="addAccount" subflow="createAccount">

<transition on="accountCreated" to="reviewBooking">

<evaluate expression="bookingService.add(…)" />

</transition>

<transition on="creationCancelled" to="end" />

</subflow-state>

Dmitry Noskov Spring Framework - Web Flow

Page 42: Spring Framework - Web Flow

Transition

Dmitry Noskov Spring Framework - Web Flow

Page 43: Spring Framework - Web Flow

Advanced

<transition on="cancel"

on-exception=""

to="cancel"

bind="true"

validate="true“

history="preserve/discard/invalidate"/>

Dmitry Noskov Spring Framework - Web Flow

Page 44: Spring Framework - Web Flow

Global transition

<global-transitions>

<transition on="cancel" to="cancel" />

</global-transitions>

Dmitry Noskov Spring Framework - Web Flow

Page 45: Spring Framework - Web Flow

Finish

Dmitry Noskov Spring Framework - Web Flow

Page 46: Spring Framework - Web Flow

End state

simple <end-state id="cancel" />

custom view <end-state id="success"

view="externalRedirect:controller?result=success"/>

output <end-state id="bookingConfirmed">

<output name="bookingId" value="booking.id" />

</end-state>

Dmitry Noskov Spring Framework - Web Flow

Page 47: Spring Framework - Web Flow

Redirects

servletRelative

contextRelative

serverRelative

http:// or https://

Dmitry Noskov Spring Framework - Web Flow

Page 48: Spring Framework - Web Flow

FlowHandler

Interface public interface FlowHandler {

public String getFlowId();

public MutableAttributeMap createExecutionInputMap(…);

public String handleExecutionOutcome(…);

public String handleException(…);

}

Bean <bean name="hotels/booking" class="test.BookingFlowHandler"/>

Dmitry Noskov Spring Framework - Web Flow

Page 49: Spring Framework - Web Flow

Spring JS

Dmitry Noskov Spring Framework - Web Flow

Page 50: Spring Framework - Web Flow

Configuration

server side <mvc:resources mapping="/resources/**"

location="/, classpath:/META-INF/web-resources/"/>

<mvc:default-servlet-handler />

client side <script type="text/javascript"

src="<c:url value="/resources/dojo/dojo.js" />"></script>

<script type="text/javascript"

src="<c:url value="/resources/spring/Spring.js" />"></script>

<script type="text/javascript"

src="<c:url value="/resources/spring/Spring-Dojo.js"/>"></script>

Dmitry Noskov Spring Framework - Web Flow

Page 51: Spring Framework - Web Flow

Simple decoration

<form:input id="searchString" path="searchString"/>

<script type="text/javascript">

Spring.addDecoration(new Spring.ElementDecoration({

elementId : "searchString",

widgetType : "dijit.form.ValidationTextBox",

widgetAttrs : {promptMessage : "Search hotels by name."}

}));

</script>

Dmitry Noskov Spring Framework - Web Flow

Page 52: Spring Framework - Web Flow

Regexp

<form:input path="creditCard"/>

<script type="text/javascript">

Spring.addDecoration(new Spring.ElementDecoration({

elementId : "creditCard",

widgetType : "dijit.form.ValidationTextBox",

widgetAttrs : {

required : true,

invalidMessage : "A 16-digit card number is required.",

regExp : "[0-9]{16}"

}

}));

</script>

Dmitry Noskov Spring Framework - Web Flow

Page 53: Spring Framework - Web Flow

Date

<form:input path="checkoutDate"/>

<script type="text/javascript">

Spring.addDecoration(new Spring.ElementDecoration({

elementId : "checkoutDate",

widgetType : "dijit.form.DateTextBox",

widgetAttrs : {

datePattern : "MM-dd-yyyy",

required : true

}

}));

</script>

Dmitry Noskov Spring Framework - Web Flow

Page 54: Spring Framework - Web Flow

Conclusion

handles all navigation steps (including back, refresh)

clear state management

full integration with Spring MVC and other

Dmitry Noskov Spring Framework - Web Flow

Page 55: Spring Framework - Web Flow

Books

Dmitry Noskov Spring Framework - Web Flow

Page 57: Spring Framework - Web Flow

Questions

Dmitry Noskov Spring Framework - Core

Page 58: Spring Framework - Web Flow

The end

http://www.linkedin.com/in/noskovd

http://www.slideshare.net/analizator/presentations