Meyyappan has posted 101 posts at DZone. View Full User Profile

Spring MVC Framework Tutorial

  • submit to reddit

Spring MVC helps in building flexible and loosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end services.

The figure below shows the flow of request in the Spring MVC Framework.

When a request is sent to the Spring MVC Framework the following sequence of events happen.

  • The DispatcherServlet first receives the request.
  • The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request.
  • The Controller process the request by calling the appropriate service methods and returns a ModeAndView object to the DispatcherServlet. The ModeAndView object contains the model data and the view name.
  • The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke.
  • Now the DispatcherServlet will pass the model object to the View to render the result.
  • The View with the help of the model data will render the result back to the user.

To understand the Spring MVC Framework we will now create a simple hello world example using the Eclipse IDE. I am using Exclipse IDE 3.4 , Spring IDE plugin, Tomcat 6.0 and Spring 3.0 to demonstrate this example.

Go to File -> New -> Dynamic Web Project, to create a web project.


 Enter the project name and click the Finish button.

 Right click the project folder, and select Spring Tools -> Add Spring Project Nature, to add Spring capabilities to the web project. This feature will be available once you install the Spring IDE.

Create a new package com.vaannila inside the src directory. The Spring controller class extends org.springframework.web.servlet.mvc.AbstractController class. To create a new controller class right click the src directory and create a new java class, enter the controller class name and super class name and the Finish button. 


 Copy the following code inside the HelloWorldController class.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

    private String message;
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("welcomePage","welcomeMessage", message);
    public void setMessage(String message) {
        this.message = message;



 The DispatcherSevlet, as the name indicates, is a single servlet that manages the entire request-handling process. When a request is sent to the DispatcherServlet it delegates the job by invoking the appropriate controllers to process the request. Like any other servlet the DispatcherServlet need to be configured in the web deployment descriptor as shown.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>

Here the servlet name is dispatcher. By default the DispatcherServlet will look for a file name dispatcher-servlet.xml to load the Spring MVC configuration. This file name is formed by concatenating the servlet name ("dispatcher") with "-servlet.xml". Here we user the the url-pattern as ".htm" inorder to hide the implementations technology to the users.

The redirect.jsp will be invoked first when we execute the Spring web application. This is the only jsp file outside the WEB-INF directory and it is here to provide a redirect to the DispatcherServlet. All the other views should be stored under the WEB-INF directory so that they can be invoked only through the controller process.

To create a bean configuration file right click the WebContent folder and select New -> Other. The following dialog box appears.


Select the Spring Bean Configuration file and click Next. 


Enter the file name as "dispatcher-servlet.xml" and click the Finish button.

Now the Spring bean configuration file is created, we need to configure the Controller and the ViewResolver classes. The following code shows how to do this.

    <bean id="viewResolver"
    class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
        <property name="prefix">
        <property name="suffix">

    <bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
        <property name="message" value="Hello World!" />


First let's understand how to configure the controller.

<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
    <property name="message" value="Hello World!" />

Here the name attribute of the bean element indicates the URL pattern to map the request. Since the id attribute can't contain special characters like "/" , we specify the URL pattern using the name attribute of the bean element. By default the DispatcherServlet uses the BeanNameUrlHandlerMapping to map the incoming request. The BeanNameUrlHandlerMapping uses the bean name as the URL pattern. Since BeanNameUrlHandlerMapping is used by default, you need not do any seperate configuration for this.

We set the message attribute of the HelloWorldController class thru setter injection. The HelloWorldController class is configured just like an another JavaBean class in the Spring application context, so like any other JavaBean we can set values to it through Dependency Injection(DI).

The redirect.jsp will redirect the request to the DispatcherServlet, which inturn consults with the BeanNameUrlHandlerMapping and invokes the HelloWorldController. The handleRequestInternal() method in the HelloWorldController class will be invoked. Here we return the message property under the name welcomeMessage and the view name welcomePage to the DispatcherServlet. As of now we only know the view name, and to find the actual view to invoke we need a ViewResolver.

The ViewResolver is configured using the following code.

<bean id="viewResolver"
class=" org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
    <property name="suffix">

Here the InternalResourceViewResolver is used to resolve the view name to the actual view. The prefix value + view name + suffix value will give the actual view location. Here the actual view location is /WEB-INF/jsp/welcomePage.jsp

The following library files are needed to run the example.


 To execute the example run the redirect.jsp file. The following page will be displayed.



 You can download and try the Spring MVC example by clicking the Download link below.

Source:  Download

War:  Download


Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Miz Big replied on Tue, 2012/11/13 - 12:30pm

Thanks for the tutorial, its nice and easy to follow.

I have one little advice, please move the lib files section "the following lib files are needed to run the example" to the top as i was expecting that if i add spring project nature, i thought it will include all the jar files in the project but we have explicitly add all the jar files.



Yogs Figure replied on Thu, 2013/01/17 - 10:57pm

I agree with previous comment; spring ide was not installed so anyway I did not use it.

Can you also add the JSP file contents, how the welcome message retrieved from ModelandView and displayed on the page?

Vishal S M replied on Thu, 2013/02/28 - 7:23am

Thank you for the tutorial, i have doubt in this.Where is the HandlerMapping and what it is?We dont have access to it?How will it come to know which controller to invoke?

Smitha Dsouza replied on Thu, 2013/09/19 - 1:14am

 Thank you so much.Good Job

I tried this tutorial..

Nilesh Mhatre replied on Sun, 2013/10/20 - 10:52pm

Hi All,

I tried the above tutorial but am unable to run the same due to below exception.
Could someone please help me on this.

 java.lang.NoClassDefFoundError: org/springframework/http/converter/HttpMessageConverter
   at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.<init>(
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(

Many Thanks,

Zue Hidden replied on Fri, 2013/11/15 - 4:52am


When I run the project that contains your example, I get an error such as; org.springframework.web.servlet.DispatcherServletat org.apache.catalina.loader.WebappClassLoader.loadClass( org.apache.catalina.loader.WebappClassLoader.loadClass( org.apache.catalina.core.DefaultInstanceManager.loadClass( org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged( org.apache.catalina.core.DefaultInstanceManager.newInstance( org.apache.catalina.core.StandardWrapper.loadServlet( org.apache.catalina.core.StandardWrapper.load( org.apache.catalina.core.StandardContext.loadOnStartup( org.apache.catalina.core.StandardContext.startInternal( org.apache.catalina.util.LifecycleBase.start( org.apache.catalina.core.ContainerBase$ org.apache.catalina.core.ContainerBase$ Source)at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)at java.util.concurrent.ThreadPoolExecutor$ Source)at Source)

Although, I add all jar files of spring framework which recently downloaded from the maven repository , I cannot get a solution to handle the error.  Do you have any idea about it?

Pradeep Yadav replied on Sat, 2014/07/05 - 2:28pm

thanks a lot for good post 

Ravi Bhatia replied on Mon, 2014/07/28 - 6:17pm


Yashpal Patil replied on Tue, 2014/08/12 - 12:48am

I have one per my understanding from above example redirect.jsp will redirect the request to the DispatcherServlet, which inturn consults with theBeanNameUrlHandlerMapping and invokes the HelloWorldController. But I want to know is there anything that runs the  DispatcherServlet while compiling the code???

Koushik Pal replied on Fri, 2014/11/21 - 5:21am


I am very new to Spring MVC. This is really helpfull post to creating a Spring MVC project, but I am not able to understand two things-

1. How you created "welcome.htm" dynamically?

2. How you will take the values form .jsp or .html page to dispatcher-servlet.xml dynamically? [ You have takken the value as "Hello World!" ] .



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.