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.
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.
Here's a very simple Spring MVC controller. We'll be updating this over the course of the article.
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.
Here's the home page JSP I just mentioned. Like
CartController, we'll be updating this.
/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>
Here's our web.xml file:
/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.
Here's mycart-servlet.xml, which
DispatcherServlet loads as just explained:
/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.
At this point you should be able to deploy the application. Point your browser at
http://localhost:8080/mycart1/home.doand 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.