Define Flow

From Documentation


Purpose

Define and configure Spring Webflow to be used with ZK Spring Webflow.

Example

For introducing ZK Spring Webflow we have borrowed from standard Spring Webflow booking-mvc application that you can download from here. We have modified it to use ZK as a presentation layer and also demonstrate ZK Spring integration library features.

You can see this sample application in action below


It has two flows;main and booking. Main flow is what users use to search hotels and view their details. Booking flow is what users use to book specific hotel by registering themselves and entering their booking details.

Before we define and implement these webflows we need to configure Spring Webflow to be able to discover these cofigurations. Lets see how we can achieve that in the following section.

Configuration

First we register our flows definitions with Spring Webflow flowRegistry in webflow-config.xml as shown below

    <!-- The registry of executable flow definitions -->
    <webflow:flow-registry id="flowRegistry"
        flow-builder-services="zkFlowBuilderServices">
        <webflow:flow-location id="main" path="/WEB-INF/flows/main/main.xml" />
        <webflow:flow-location id="booking" path="/WEB-INF/flows/booking/booking.xml" />
    </webflow:flow-registry>

This example uses persistence layer to store booking information in the database and it is achieved by using Spring Webflow's org.springframework.webflow.persistence.JpaFlowExecutionListener.

    <!-- Executes flows: the central entry point into the Spring Web Flow system -->
    <webflow:flow-executor id="flowExecutor">
        <webflow:flow-execution-listeners>
            <webflow:listener ref="jpaFlowExecutionListener" />
            <webflow:listener ref="securityFlowExecutionListener" />
        </webflow:flow-execution-listeners>
    </webflow:flow-executor>

...

    <!-- Installs a listener that manages JPA persistence contexts for flows that require them -->
    <bean id="jpaFlowExecutionListener"
        class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
        <constructor-arg ref="entityManagerFactory" />
        <constructor-arg ref="transactionManager" />
    </bean>


As described in the previous section, this example uses Spring Webmvc to implement main and booking flows. We define flow request URIs to their Spring MVC controllers mappings in webmvc-config.xml as shown below

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Maps request URIs to controllers -->             
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /main=flowController
                /booking=flowController
            </value>
        </property>
        <property name="defaultHandler">
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
        </property>
    </bean>
    
    <!-- Maps logical view names to ZK templates (e.g. 'search' to '/WEB-INF/search.zul' -->
    <bean id="viewResolver" class="org.zkoss.spring.web.servlet.view.ZkResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".zul"/>
    </bean>
</beans>

Flow definitions

Now moving on to the actual flow definitions, first we define main flow that used to search for hotels and view hotel details. There are four view states; enterSearchCriteria, reviewHotels, reviewHotel and changeSearchCriteria. It also defines a subflow state bookHotel that refers to booking subflow (defined in booking.xml). Lets take a look at a gragment from main.xml that defines enterSearchCriteria view state and booking subflow.

<?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">
    
    <var name="searchCriteria" class="org.springframework.webflow.samples.booking.SearchCriteria" />
    
    <view-state id="enterSearchCriteria">
        <on-render>
            <evaluate expression="bookingService.findBookings(currentUser.name)" result="viewScope.bookings" />
        </on-render>
        <transition on="search" to="reviewHotels">
            <evaluate expression="searchCriteria.resetPage()" />
        </transition>
        <transition on="cancelBooking">
            <evaluate expression="bookingService.cancelBooking(componentScope.booking)" />
            <render fragments="bookingsFragment" />
        </transition>
    </view-state>
...    
...
    <subflow-state id="bookHotel" subflow="booking">
        <input name="hotelId" value="hotel.id" />
        <transition on="bookingConfirmed" to="finish" />
        <transition on="bookingCancelled" to="enterSearchCriteria" />
    </subflow-state>
...         
    <end-state id="finish" />
   
</flow>

You can see the full source code of main.xml here.

As you can see the main flow starts with earching for hotel i.e. by viewing enterSearchCriteria state. <on-render /> child element of it specifies certain back-end operation such as booking search to be performed on rendering the state. It also specifies if the result of this operation is stored into a variable in a specific scope such as bookings in viewScope. <view-state /> elements also specify transitions to other states using <transition /> child elements. This main flow also starts a booking subflow as specified by <subflow-state /> element.

the booking sub-flow is defined in a similar way with certain view-states and their transitions. you can take a look at booking.xml that defines this subflow here


Version History

Last Update : 2022/01/19


Version Date Content
     


Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.