Spring MVC - Application Setup

Maven Setup for Spring MVC - POM Dependency

Add the following dependency to a Maven POM file

<dependency>
    <groupId>org.springframework.webflow</groupId>
    <artifactId>spring-webflow</artifactId>	
    <version>2.0.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.webflow</groupId>
    <artifactId>spring-binding</artifactId>
    <version>2.0.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.webflow</groupId>
    <artifactId>spring-js</artifactId>
    <version>2.0.8.RELEASE</version>
</dependency>

<!-- Add a taglib for the sample code -->
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

<!-- For tile support. Spring 2.5.6 needs Apache Tile 2.0.7 -->
<!-- It does not support 2.1 or later -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>2.0.7</version>
</dependency>

Spring MVC Application Directory Structure

Create a Spring MVC application with the following directory structure

springweb                 # The name of the Spring application
   - src/main/java        # Root of source code
   - src/main/resources   # Property & configuration file
   - src/test/java        # Root of testing source code
   - src/test/resources   # Property & configuration file for Testing
   - src/main/webapp/WEB-INF/conf     # Home to Spring, Spring MVC & Spring Webflow configuration
   - src/main/webapp/WEB-INF/jsp      # Root of JSP pages 
   - src/main/webapp/WEB-INF/layouts  # Root of Tile layout template 
   - src/main/webapp/WEB-INF/web.xml  # Web application configuration

Spring MVC Application Configuration

Web Application Configuration - web.xml

Create a web.xml containing Spring MVC configuration - src/main/webapp/WEB-INF/web.xml

<?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_5.xsd"
	metadata-complete="true" version="2.5">

    <display-name>Spring Web Application</display-name>

    <!-- Spring web application configuration file location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>
		/WEB-INF/conf/web-application-config.xml
	    </param-value>
    </context-param>

    <!-- Loads the Spring web application context -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <!-- Front controller for Spring MVC -->
    <!-- Dispatch requests to application business code (Controller) -->
    <servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
	    <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Handling http://domain.com/app_context_root/* request -->
    <!-- Or http://domain.com/* if the application is deployed as / -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Spring web application configuration - web-application-config.xml

Create a Spring configuration file - src/main/webapp/WEB-INF/conf/web-application-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config />
	
    <!-- Spring MVC configuration -->
    <import resource="webmvc-config.xml" />

</beans>

Spring MVC Application Configuration - webmvc-config.xml

Create a Spring MVC configuration file - src/main/webapp/WEB-INF/conf/webmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <!-- Required for Spring MVC annotation -->
    <!- Enable Basic MVC Annotation (map URL to controller's method) at class/method level ->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
    <!- Enable more MVC Annotation at method level ->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

</beans>