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"?>
<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_3_0.xsd"
    id="WebApp_ID"
    version="3.0">
    
    <display-name>Spring3 MVC Hello World Annotation</display-name>
    
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>	
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

/WEB-INF/web.xml

 

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="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     
    <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" />
    </bean>
</beans>

/WEB-INF/dispatcher-servlet.xml

Notes:

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.

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

/WEB-INF/jsp/hello-world.jsp

 

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;

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

/src/com/consistentcoder/controllers/Main.java

 

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/