Develop a Spring3 MVC Hello World Annotation Application

May 17, 2013 | Last tested: September 2015 | 732 views | Comments

A simple "Hello World" example usingSpring3 MVC JAVA framework driven by the Annotation element.


Built and tested with the following:

  • Eclipse Juno IDE
  • Apache Tomcat 7.0.47
  • JDK 1.7.0

Required jars:


Step 1. Create a new Project.

Click File -> New -> Dynamic Web Project.

Enter "Spring3 MVC Hello World Annotation" on the "Project name:" field, and click "Finish".


Step 2. Create the Deployment Descriptor file.

Inside the "WEB-INF" folder, create the "web.xml" file, and put the following code.

<?xml version="1.0" encoding="UTF-8"?>
    <display-name>Spring3 MVC Hello World Annotation</display-name>



Step 3. Create the Spring DispatcherServlet Configuration file.

Still inside the "WEB-INF" folder, create another XML file, name it "dispatcher-servlet.xml", and copy the following code inside it.

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    <context:component-scan base-package="com.consistentcoder.controllers" />
    <mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />



The XML file (dispatcher-servlet.xml) must be created based on the servlet-name value of the DispatcherServlet class that can be found on "web.xml" file. And since the value is dispatcher, the name of the XML file should be dispatcher-servlet.xml, because the default filename format is [servlet-name]-servlet.xml.

The code context:component-scan base-package="com.consistentcoder.controllers" tells Spring to look for annotated classes inside the package com.consistentcoder.controllers

The code mvc:annotation-driven indicates the support to annotation-driven Controller methods like @Controller. This would not need an XML configuration to define Spring bean dependencies.

Inside the bean viewResolver, View configurations such as the file type (.jsp) and path (/WEB-INF/jsp/) were set.


Step 4. Create the View file.

Create a new folder and name it jsp inside the folder WEB-INF, and create a new .jsp file inside it, name it "hello-world.jsp". Inside the newly created jsp file, put the following code.

Note: On the file dispatcher-servlet.xml, template files with the extension of .jsp are configured to be located inside the folder jsp under WEB-INF.

    <title>Spring3 MVC Hello World Annotation Example</title>
    <h1>${ message }</h1>



Step 5. Create the Controller class.

First, create a new package inside src folder under the Java Resources folder , name it com.consistentcoder.controllers.

Right click on src, select New -> Package.

Then enter com.consistentcoder.controllers on the Name: field, and click Finish

The package com.consistentcoder.controllers has been created.

Now that the package has been created, the actual Controller class can be made.

Right click on the package com.consistentcoder.controllers, select New -> Class.

Then enter Main on the Name: field, and click Finish

Main class has been created.

Copy the code below inside the newly created java Main class.

package com.consistentcoder.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

public class Main {
    public String index(ModelMap model) {
        model.addAttribute("message", "Spring3 MVC Hello World Annotation Example!");
        return "hello-world";



That's all!

Spring3 MVC Hello World Annotation has been developed.


Final Project Structure


What's Next? Test the application.

Start your server and browse this link. http://localhost:8080/Spring3MVCHelloWorldAnnotation/