del.icio.us Digg DZone Reddit StumbleUpon
Tutorial: Build a Shopping Cart with Spring Web Flow 2.0 - Willie Wheeler
« Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Next »

Building Out the Application

Now that we've gotten Spring MVC and SWF working, it's time to build out the shopping cart application. Please download the following file before continuing:

We won't be looking at every aspect of this application, but I'll highlight the most interesting pieces from a SWF point of view.

Dependencies for mycart3.zip

Besides the dependencies we had for mycart2.zip, there are four others you'll need:

After you download mycart3.zip (and its dependencies), you can deploy it and then point your browser at

http://localhost:8080/mycart/home.do

Here are some notes to keep in mind:

  • My registration and login flows don't actually "do" anything. They're not hooked up to Spring Security and they're not hooked up to a database. So when you register you can just hit the submit button, and when you log in you can just hit the login button. I just wanted to focus on the flows themselves.
  • I'm using Sitemesh to ensure a unified layout across the pages. Even if you are not familiar with Sitemesh, don't worry: it is very straightforward. Basically you just put a servlet filter in front of the pages and the filter decorates the pages with a template (/WEB-INF/jsp/pagetemplate.jsp in this case) according to a simple configuration file (/WEB-INF/decorators.xml). If however you don't want to use Sitemesh, simply remove the filter from web.xml and Sitemesh is gone.

Multiple flows

Any given app may contain multiple flows, and mycart3 is such an app. We have four different flows:

  • addToCart: add an item to a shopping cart
  • checkout: shopping cart checkout process
  • login: log into the app
  • register: register for a new user account

In some of the cases we have what we would intuitively consider a flow in that there are multiple states involved; in other cases there is only one state to speak of and so it may seem strange to regard the flow as being a flow. However we'll see how that can make sense in the following section.

To create multiple flows, you will need to do the following. First, create the flow definition files and put them in your /WEB-INF/flows directory (or wherever you decided to put them). Then add the flow locations to the flow registry, and the flow URL mappings to your SimpleUrlHandlerMapping (if that's what you're using), in your Spring application context, like so:

Code listing: /WEB-INF/mycart-servlet.xml
...

<bean id="flowUrlMappings" class=
        "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /addToCart.do=flowController
            /checkout.do=flowController
            /account/login.do=flowController
            /account/register.do=flowController
        </value>
    </property>
    <property name="alwaysUseFullPath" value="true"/>
</bean>

<flow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <flow:flow-location path="/WEB-INF/flows/addToCart.xml"/>
    <flow:flow-location path="/WEB-INF/flows/checkout.xml"/>
    <flow:flow-location path="/WEB-INF/flows/login.xml"/>
    <flow:flow-location path="/WEB-INF/flows/register.xml"/>
</flow:flow-registry>

...

Once you have those in place you should be set up for multiple flows. Point your links at them and try them out!

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
« Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Next »
Show comments (62)

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Please help us prevent comment spam:

What's New?

2008-10-20 - I've added a new mailing list feature to the site. Sign up to receive e-mail updates about new articles.
2008-09-30 - We've released chapter 4 (User registration) and chapter 5 (Authentication) of Spring in Practice.
2008-09-11 - By popular demand, I've added an RSS feed to the site.
Home | Consulting | Tech Articles | Mailing List | About | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.