In this Spring MVC Tutorial (3.0), I am going to give you a start up code base for a Spring MVC 3 based application. If you want to go back and refer to the previous version of Spring MVC then this article will help you. I am assuming that the reader is already aware of Java 5 and Spring basic concepts like annotations, dependency injection etc. Spring 3.0 has extensively used the popular annotation feature of Java 5. As we move ahead in this article, I’ll mention more new things wherever applicable.

It will take following five steps to make everything work on eclipse and tomcat.

  1. Eclipse project set up
  2. Addition of required jars
  3. Creation of required code elements
  4. Running project on tomcat
  5. Testing everything

Let us go through each step in detail.

Spring MVC Tutorial in Detail:

1. Eclipse Project Set Up:

Some of us may think that this is an elementary, but I am mentioning it for those who are new to eclipse. In this step we create a dynamic web project with name SpringMVC3Tutorial. You can use any Eclipse version; most of the recent versions are having these steps similar. I am using Eclipse Galileo for JEE ( from eclipse download site). This contains many advanced features, which help in development as well as deployment of application on different servers.

Eclipse menu File -> New -> Project will open this window. Select Dynamic Web Project from it and click on next.

Spring MVC Tutorial - Eclipse Project Setup

It will open following window where you specify the project name as ‘SpringMVC3Tutorial’ and select the server on which you want to deploy application. I am selecting tomcat 6.0 as it is available on my machine. You can configure server as per availability. Click next on following screen.

Now change the source folder from ‘src’ to ‘src\main\java’. This will help us to keep main and test java classes separate. Later build process becomes easy with build tools like ant and maven. It is also maintainable.

Also on same screen, change the default output folder. This generates the classes at right place with respect to web archive (war). Click next button after that.

Configure web module here. We keep it as ‘src\main\webapp’.

Clicking on finish button will generate complete project structure for us. It will also generate web.xml which we’ll modify later.

2. Addition of Required Jars:

Let us compare the jars we used in Spring 2.5 and in current version of Spring which is 3.0.

Spring 2.5 Jars:










Spring 3.0 Jars:











Jar names in italics are new requirements as compared to previous versions. *asm*.jar is newly added in this stack. Instead of *webmvc*.jar now we have *web.servlet*.jar.

Add these jars to ‘src\main\webapp \WEB-INF\lib’ folder.

These jars can be downloaded from Spring’s site or from Maven repository.

3. Creation of Required Code Elements:

Using above two steps, we have created the skeleton we require to write our Spring MVC application. Now we identify the code elements we require to make simple hello world web application work.



Some Changes to web.xml


This is the controller class or what we call ‘C’ in the MVC pattern. If we take a closer look at the code below then we can notice following things.

- It is not extending from any framework class but using annotation @Controller.

- There is no specific method to be used to handle request. We have our business method helloWorld() which is doing the request handling.

- Request processing method helloWorld() is mapped to request pattern /helloWorld (actually it will be ‘http://localhost:8080/SpringMVC3Tutorial/helloWorld.htm’).

There are more annotations around controller, request mapping, and session handling etc. We leave those for more detailed discussion.

<br />package com.myorg.springmvctutorial.web.controller;<br /><br />import org.springframework.stereotype.Controller;<br />import org.springframework.web.bind.annotation.RequestMapping;<br />import org.springframework.web.servlet.ModelAndView;<br /><br />import org.apache.commons.logging.Log;<br />import org.apache.commons.logging.LogFactory;<br /><br />@Controller<br />public class HelloWorldController{<br />	protected final Log logger = LogFactory.getLog(getClass());<br /><br />	@RequestMapping("/helloWorld")<br />	public ModelAndView helloWorld(){<br />"Return View");<br />		return new ModelAndView("helloworld.jsp");<br />	}<br />}<br />


This is a simple html page which will confirm that everything is integrated and working fine.

<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;Hello World&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1&gt;This is Spring MVC 3 Tutorial&lt;/h1&gt;<br />&lt;p&gt;Our Tutorial is successful.&lt;/p&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />

\src\main\webapp\WEB-INF \SpringMVC3Tutorial-servlet.xml:

This file contains all bean definitions. In our example, it should contain xml like this –

<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns=""<br />	xmlns:xsi=""<br />	xsi:schemaLocation="<br />"&gt;<br /><br />	&lt;bean name="/helloworld.htm"<br />		class="com.myorg.springmvctutorial.web.controller.HelloWorldController" /&gt;<br />&lt;/beans&gt;<br />

But we are going to have something different. Instead of explicit definitions like this, we use following two features –

- RequestMapping annotation used in controller class above which maps helloWorld() method to /helloWorld url.

- Autowiring feature of spring.

Also this xml will be validated using newly provided xsds in Spring 3.0.

<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;beans xmlns=""<br />	xmlns:xsi="" xmlns:p=""<br />	xmlns:context=""<br />	xsi:schemaLocation="<br /><br /><br /><br />"&gt;<br />	&lt;context:component-scan base-package="com.myorg.springmvctutorial.web.controller" /&gt;<br />&lt;/beans&gt;<br />

\src\main\webapp\WEB-INF \web.xml:

It cannot have only the context root definition now. Entire Spring MVC web application runs on DispatcherServlet. We need to define this servlet in web.xml. Here is how we define it.

<br />&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;web-app xmlns:xsi=""<br />	xmlns="" xmlns:web=""<br />	xsi:schemaLocation=""<br />	id="WebApp_ID" version="2.5"&gt;<br />	&lt;display-name&gt;SpringMVC3Tutorial&lt;/display-name&gt;<br />	&lt;servlet&gt;<br />		&lt;servlet-name&gt;SpringMVC3Tutorial&lt;/servlet-name&gt;<br />		&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;<br />		&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;<br />	&lt;/servlet&gt;<br />	&lt;servlet-mapping&gt;<br />		&lt;servlet-name&gt;SpringMVC3Tutorial&lt;/servlet-name&gt;<br />		&lt;url-pattern&gt;*.htm&lt;/url-pattern&gt;<br />	&lt;/servlet-mapping&gt;<br />	&lt;welcome-file-list&gt;<br />		&lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;<br />	&lt;/welcome-file-list&gt;<br />&lt;/web-app&gt;<br />

4. Running Project on Tomcat:

You can choose what you like from following options.

- Use tomcat integration with eclipse and deploy using right click Run -> Run on Server option.

- Export project as a war using File -> Export menu option and copy generated war file in webapps directory of tomcat.

- Use any build tool like ant, maven to generate the war file and copy it to webapps directory on tomcat.

5. Testing Everything:

To see if everything is integrated and working fine in this Spring MVC Tutorial, access this url http://localhost:8080/SpringMVC3Tutorial/helloWorld.htm in browser and we see following page displayed.


Spring MVC 3.0 Documentation

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS