Spring 3 MVC Hello World Example

Reading Time: 2 minutes

In this simple tutorial I will show you how to create the folder structure, add necessary libraries and write code of Spring 3 MVC. First off all your project has to have the following jars to be imported in your project minimum:

  • antlr-2.7.6.jar
  • commons-lang-2.4.jar
  • commons-logging-1.1.1.jar
  • jstl.jar
  • log4j-1.2.11.jar
  • org.springframework.beans-3.0.6.RELEASE.jar
  • org.springframework.context-3.0.6.RELEASE.jar
  • org.springframework.core-3.0.6.RELEASE.jar
  • org.springframework.expression-3.0.6.RELEASE.jar
  • org.springframework.web-3.0.6.RELEASE.jar
  • org.springframework.web.servlet-3.0.6.RELEASE.jar

This is here how your project folder needs to look like:


Add these necessary jars into lib folder, to do it please drag them into the folder so that you won’t have to kill yourself how to. Prohibit yourself from adding the jars as classpath.

So let’s get our hands into codes, section by section I will be giving you the explanations what all the files mean and stand for. First of all create a Dynamic web project with a name called “Spring3MVC” or the way you want. Second of all drag the necessary jars into lib folder and let us get started with the code. First of all create a new folder called “jsp” right under “WEB-INF” folder.

Right click on the project and create a new class called “HelloWorldController.java” and type the package as “com.example.controller”, the below the code comes:

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class HelloWorldController {

    public ModelAndView helloWorld() {

        String message = "Hello World, this is Spring 3.0 MVC!";
        return new ModelAndView("hello", "message", message);

This is our controller code, this will be residing in Java Resources src folder which is where it is supposed to be, so let’s move on and create a our jsp files. Right click on the project and create a new jsp file called “index.jsp” under “WebContent” please be aware this index file should not be in “jsp” folder, and paste this code into the file

    <title>Spring 3.0 MVC Hello World Sample</title>
    <a href="hello.html">Say Hello</a>

and now the same way create a new folder in “jsp” folder called “hello.jsp” and paste the code below

    <title>Spring 3.0 MVC Hello World Sample</title>

Its time to create our configuration files which will be in charge of managing our project. First of all let us create the “web.xml” file under “WEB-INF” folder and paste the code below

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
    id="WebApp_ID" version="2.5">


and now only one file left to be created our spring configuration file called “spring-servlet.xml” please create this folder the same way right underneath the folder “WEB-INF” and paste this code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

        base-package="com.example.controller" />

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

and now we are pretty much ready to deploy the code to our application server. Please go ahead and give it a shot, and I will below be explaining what these files stand for we newly created.

You can download the full project hereĀ Spring3MVC