Components of the itemlist Spring Web Flow Application
The first thing to note about the itemlist application is that it's a servlet—more precisely, it's a regular Spring MVC DispatcherServlet (the central dispatcher for HTTP request handlers/controllers). Listing 1 shows the contents of the itemlist application's web.xml file.
Listing 1 The web.xml file.
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <servlet> <servlet-name>itemlist</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>itemlist</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Nothing too surprising in Listing 1. The main welcome page is the index.jsp page illustrated in Listing 2.
Listing 2 The index.jsp page.
<%@ page session="true" %> <%-- make sure we have a session --%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> </HEAD> <BODY> <DIV align="left">Item List - A Spring Web Flow Sample</DIV> <HR> <DIV align="left"> <P> <A href="app/itemlist">Item List</A> </P> <P> <A href="app/itemlist-alternate">Item List Alternate</A> (same flow with the 'add item' process modeled as a subflow) </P> <P> This Spring web flow sample application illustrates several features: </P> <UL> <LI> Launching flow's using bookmark-friendly, REST-style URLS </LI> <LI> Use of an inline-flow, including the ability to map subflow output attributes directly into collection attributes in parent flow scope. </LI> <LI> Event pattern matching, for matching eventId expressions to transitions. </LI> <LI> "Always redirect on pause" - to achieve the POST+REDIRECT+GET pattern with no special coding. </LI> <LI> Spring 1.2 compatible configuration, as an alternative to the Spring 2.0 support. </LI> </UL> </DIV> <HR> <DIV align="right"></DIV> </BODY> </HTML>
If you compare the contents of Listing 2 with the text in Figure 1, you'll see that this is the main launch pad for the itemlist application. Take a look at the following line from Listing 2:
<A href="app/itemlist">Item List</A>
The href tag is a clickable link that refers to a flow definition as defined in the web flow XML file (called itemlist.xml) in Listing 3. This is really the core of the web flow application—the state definitions.
Listing 3 The itemlist.xml file.
<?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-1.0.xsd"> <var name="list" class="java.util.ArrayList" /> <start-state idref="displayItemlist" /> <view-state id="displayItemlist" view="itemlist"> <transition on="add" to="createItem" /> </view-state> <action-state id="createItem"> <action bean="createItemAction" /> <transition on="success" to="displayItem" /> </action-state> <view-state id="displayItem" view="item"> <transition on="submit" to="addItem" /> </view-state> <action-state id="addItem"> <action bean="addItemAction" /> <transition on="*" to="displayItemlist" /> </action-state> </flow>
I find the easiest way to get to grips with a web flow description is to run through it in your head first. (We'll take a detailed look shortly.) The various states described in Listing 3 represent the navigational view and action logic of the application. The start state occurs when the program commences (that is, when you click the Item List link shown in Figure 1).
Notice in Listing 3 the two state types: view and action. This concept is fairly intuitive: A view simply represents something that the user can see, whereas an action relates to something that the user can do. An obvious example of an action is some type of data change, such as creating a list item. At the end of Listing 3, the final state transition is from addItem back to displayItemlist. In effect, the application is just looping around from the start state to the end state and back again to the start state.
Okay, that's the overview, now let's dig a little deeper.