20
Mar 13

Quickly creating a simple Spring MVC application

This post will cover the simple steps required to create a maven based web application using Spring. The end result will be a very basic framework that does virtually nothing, the intent is to use this as a base for other posts which will describe how we can turn this simple framework into something more functional such as a set of dynamically created web pages, or REST services.

Prerequisits

  • Java is installed
  • Maven is installed and configured corrctly

Step 1: Create a maven archytype

First create a simple maven project using the quickstart artifact as follows:

mvn archetype:generate -DgroupId=com.agitech -DartifactId=sampleapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 2: Update POM

Go into the the sampleapp directory and open the pom.xml file. First update the packaging element to the following:

<packaging>war</packaging>

By default maven uses Java 5, to update the app to use a different version of Java add the following elements:

<properties>
<java.version>1.6</java.version>
</properties>
 
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

Step 3: Add a webapps directory

Create the following directory under samplaeapp/src/main:

mkdir resources
mkdir webapp

Under the webapps directory create the directory WEB-INF:

mkdir WEB-INF

Step 4: Adding Spring

Open the pom and update the properties element defined earlier as follows:

<properties>
<java.version>1.6</java.version>
<spring.version>3.2.1.RELEASE</spring.version>
</properties>

Now add the following Spring dependencies under the <dependencies> element:

<dependency>   
<groupId>org.springframework</groupId>   
<artifactId>spring-core</artifactId>   
<version>${spring.version}</version>  
</dependency>  
<dependency>   
<groupId>org.springframework</groupId>   
<artifactId>spring-context</artifactId>   
<version>${spring.version}</version>  
</dependency>  
<dependency>   
<groupId>org.springframework</groupId>   
<artifactId>spring-web</artifactId>   
<version>${spring.version}</version>  
</dependency>  
<dependency>   
<groupId>org.springframework</groupId>   
<artifactId>spring-webmvc</artifactId>   
<version>${spring.version}</version>  
</dependency>

Step 5: Add web.xml

Create the file web.xml under samplaeapp/src/main/webapps/WEB-INF:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″ 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”>
 
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
 

The web.xml creates the DispatcherServlet used as an entry point to Spring MVC. By default the DispatchServlet will attempt to read the Spring config from /WEB-INF/{servlet-name}-servlet.xml, in this case /WEB-INF/sample-servlet.xml. To use a different name/location add an init param to the servlet definition named contextConfigLocation:

<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Step 6: Add spring XML

Create the file sample-servlet.xml under samplaeapp/src/main/webapps/WEB-INF:

<beans xmlns=”http://www.springframework.org/schema/beans”        
xmlns:context=”http://www.springframework.org/schema/context”        
xmlns:mvc=”http://www.springframework.org/schema/mvc”        
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-3.2.xsd        
http://www.springframework.org/schema/context        
http://www.springframework.org/schema/context/spring-context-3.2.xsd        
http://www.springframework.org/schema/mvc        
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd”>      
  
<context:component-scan base-package=”com.agitech.sample.controllers” />        
<mvc:annotation-driven />
</beans>

This config provides a very basic setup which scans the package com.agitech.sample.controllers for Controllers and enables them to be configured using annotations. If you want to allow these controllers to to forward onto JSP views add the following bean:

<bean id=”viewResolver” class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>
<property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/>
<property name=”prefix” value=”/WEB-INF/jsp/”/>
<property name=”suffix” value=”.jsp”/>
</bean>

Step 7: Test with Jetty

We can add the Jetty plugin to start the webapp in a container on port 8080. Obviously we have not yet added any functionality so there will be nothing to actually test. Add the following to the <plugins> element in your pom:

<plugin>        
<groupId>org.mortbay.jetty</groupId>        
<artifactId>maven-jetty-plugin</artifactId>        
<version>6.1.10</version>        
<configuration>                
<scanIntervalSeconds>10</scanIntervalSeconds>                
<stopKey>foo</stopKey>                
<stopPort>9999</stopPort>        
</configuration>
</plugin>

Now from the top level directory run the command

mvn clean install jetty:run

Now that we have our basic setup we can focus on adding functionality in subsequent posts.

Leave a Reply