Spring MVC Framework Tutorial

DZone 's Guide to

Spring MVC Framework Tutorial

Learn how you can use the Model-view-controller design pattern to better separate business, presentation, and navigation logic in your code.

· Java Zone ·
Free Resource

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 a 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 requests 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 processes the request by calling the appropriate service methods and returns a modelandview object to the dispatcherservlet . the modelandview 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 eclipse 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 then press 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 needs to be configured in the web deployment descriptor as shown.

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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 filename is formed by concatenating the servlet name (" dispatcher ") with " -servlet.xml ". here, we use the the url-pattern as " .htm " in order 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 that the spring bean configuration file is created, we need to configure the controller and the viewresolver classes. the following code demonstrates 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 separate configuration for this.

we set the message attribute of the helloworldcontroller class through setter injection. the helloworldcontroller class is configured just like any other 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, in turn, 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, but 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.

well, there you have it! you can download and try the spring mvc example by clicking on the links below.



ant, spring

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}