As there are multiple pages involved in the shopping flow, the data needs to be carried throughout multiple pages. If we handle this task manually it will be tedious task. We will go out of our indented business logic. To implement these kinds of tasks easily Spring provides the web flow framework.
A flow is nothing but a set of tasks to be done in a particular order. In webflow it is an xml file which tells the exactly how this flow should works. If we configure our application flow in spring web flow, the data carrying portion as well as the page navigation issues will be taken care by spring web flow itself. We can concentrate on our business logic rather than spending time on working those mundane tasks.
Spring web flow can be configured only via xml file. Currently it is not feasible to configure via only java.
The first thing to setup a web flow is configuration. A flow configuration involves below major components. Let’s go through them.
Flow executor: Flow executor is responsible to drive the flow. The syntax will be like below.
<flow:flow-executor id="flowExecutor"/>
Flow Registry: Flow registry is responsible to load the flow definitions. For example if all flows under folder WEB-INF/flows needs to be loaded it has to be configured like below.
<flow:flow-registry id="flowRegistry" base-path="/WEB-INF/flows"> <flow:flow-location-pattern value = "**/*-flow.xml"/> </flow:flow-registry>
Here one important thing to note is the url path to the flow will be determined by the path of the flow location. For example if the path of a flow is /WEB-INF/flows/order/order-flow.xml then the id will be order (relative to the base path.) So the flow will be triggered while accessing the url http://{url}/{contextpath}/order
Instead of the pattern the path of the flow file also can be configured. For example to load order-flow.xml flow it can be configured like below.
<flow:flow-registry id="flowRegistry"> <flow:flow-location path = "/WEB-INF/flows/order/order-flow.xml"/> </flow:flow-registry>
If the full path is specified then the filename will become the id of that flow. In the above case it will be order-flow . If id needs to be different from the path it can be specified with id attribute like below.
<flow:flow-location path = "/WEB-INF/flows/order/order-flow.xml" id="custom/path/flow"/>
As per the above code the flow will be triggered when the url {contextPath}/custom/path/ flow is entered.
Flow Handler Mapping : Like DispatcherServlet for controller mapping, there is a class FlowHandlerMapping for URL to flow mapping for web flow.
Flow Handler Adapter : Like Controllers for handling the requests, there is a class FlowHandlerAdapter to handle the flow requests
The next important thing is the flow. A Flow is an xml file which tells the exactly how this flow should works. A flow will have three major components.
State : As per the name it is the state of the flow, which means the flows should be in a state. There are five types of states possible.
<action-state id="saveOrder"> <evaluate expression="pizzaFlowActions.saveOrder(order)"/> <transition to="thankYou"/> </action-state>
<decision-state id="checkDeliveryArea"> <if test="pizzaFlowActions.checkDeliveryArea(customer.zipCode)" then="addCustomer" else="deliveryWarning"/> </decision-state>
<subflow-state id="order" subflow="pizza/order"> <input name="order" value="order"/> <transition on="orderCreated" to="payment"/> </subflow-state>
Transition: To move from one state to another state the transition element will be used.
<global-transitions> <transition on="cancel" to="endState"/> </global-transitions>
Data: To store and pass data through out the flow the data elements will be used. The syntax will be <var name=”customer” class=”org.span.domain.Customer”/>
That is all about the major components of the web flow. Also web flow can be secured as well. To secure a flow it has to be configured like below.
<view-state id="restricted" view="paymentdetails"> <secured attributes="ROLE_ADMIN,ROLE_FINANCE" match="all"/> </view-state>
attributes are comma separated roles. match can be ‘any’ – At least one role required or ‘all’ – All roles required .
In this section we have discussed the concepts of Spring web flow. In the next tutorial we will implement a simple web flow application to understand all the discussed concepts.
]]>