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 »

Spring MVC Setup

Since Spring Web Flow is build on Spring MVC, let's start by getting Spring MVC working. We'll create a simple home page for our shopping cart, and we'll serve this up using a plain old Spring controller (well, it will be annotated) rather than serving it using Spring Web Flow. That's because the home page itself isn't part of any particular flow; it simply provides entry points into various flows, such as creating an account, logging in, adding an item to a shopping cart, and checking out. Besides allowing us to make sure we have Spring MVC working before moving forward, this approach will also allow us to see how to integrate Spring MVC and Spring Web Flow.

For your convenience, here's a download of the minimalistic (i.e. only Spring MVC, no SWF) shopping cart we're about to take a look at:

The download above does not include its dependencies. You will need to grab those separately. I've provided the links below.

Dependencies for mycart1.zip

These are all part of the Spring 2.5.4 distribution. Spring Web Flow 2.0 requires Spring 2.5.4 or higher. [download]

  • spring.jar (located in /spring-framework-2.5.4/dist)
  • spring-webmvc.jar (located in /spring-framework-2.5.4/dist/modules)
  • commons-logging.jar (located in /spring-framework-2.5.4/jakarta-commons)

We will be adding dependencies as we progress; for the moment we're just getting Spring MVC set up.

Create a Spring MVC Controller

Here's a very simple Spring MVC controller. We'll be updating this over the course of the article.

Code listing: mycart.CartController
package mycart;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CartController {

    @RequestMapping("/home.do")
    public void doHome() {
    }
}

This controller doesn't do much at all. Basically we're using annotations to map /home.do requests to a JSP. For more details on how that works, please see my article Annotation-Based MVC in Spring 2.5.

Create a JSP

Here's the home page JSP I just mentioned. Like CartController, we'll be updating this.

Code listing: /WEB-INF/jsp/home.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html>
    <head>
        <title>Products for Geeks - GeekWarez</title>
    </head>
    <body>
        <h1>Welcome to GeekWarez</h1>
    </body>
</html>

Create web.xml

Here's our web.xml file:

Code listing: /WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    
    <!-- Spring MVC front controller. Automatically loads mycart-servlet.xml
         based on servlet name. -->
    <servlet>
        <servlet-name>mycart</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>mycart</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

All we're doing here is creating the Spring MVC DispatcherServlet front controller. Because we've named it mycart, the default behavior for DispatcherServlet is to look for a Spring application context configuration file at /WEB-INF/mycart-servlet.xml, which we are about to see.

Eventually this front controller will handle not only our "normal" non-SWF requests, but also our SWF requests. However I'm getting ahead of myself.

Create the Spring application context file

Here's mycart-servlet.xml, which DispatcherServlet loads as just explained:

Code listing: /WEB-INF/mycart-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <!-- This activates post-processors for annotation-based config -->
    <!-- http://www.infoq.com/articles/spring-2.5-part-1 -->
    <context:annotation-config/>
    
    <context:component-scan base-package="mycart"/>
    
    <!-- Enables POJO @Controllers (like CartController) -->
    <bean class=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    
    <!-- Enables annotated methods on POJO @Controllers (like CartController) -->
    <bean class=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
    
    <!-- Maps a logical view name to a physical resource -->
    <bean id="viewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Nothing special here assuming you already know Spring MVC.

Milestone 1: Spring MVC is Working

At this point you should be able to deploy the application. Point your browser at

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

and you should get a very simple home page. If so, congratulations, Spring MVC is working.

Now it's time to create our first flow using Spring Web Flow.

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:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

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.