Spring 4 MVC Hello World Annotation Example

August 07, 2015 | Updated last October, 2015 | 2746 views | Comments

Simple "Hello World" example featuring the use of Spring 4 Annotation-Driven MVC Framework.

 

Built and tested with the following:

  • Eclipse Juno IDE
  • Apache Tomcat 7.0.47
  • JDK 1.7.0
  • Maven 3.0.4
  • Spring Web-MVC Framework 4.2.2
    • Configuration: XML
    • Url Mapping: Annotation

 

Step 1. Create a new Maven Project.

Set up a new Maven Project with the following details:

  • Group Id: com.consistentcoder
  • Artifact Id: spring-4-mvc-hello-world-annotation
  • Name: spring 4 mvc hello world annotation

Tutorials:
Create a New Maven Project in Eclipse
Configure Run on Server option on a Maven Project on Eclipse IDE

After you have followed the tutorials above, your Maven Project Structure would look something like below.

 

Step 2. Edit pom.xml file.

Overwrite the code inside "pom.xml" with the following.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.consistentcoder</groupId>
    <artifactId>spring-4-mvc-hello-world-annotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>spring 4 mvc hello world annotation</name>
    
    <dependencies>
        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

/pom.xml

 

Step 3. Edit web.xml file.

Replace the code of "web.xml" file with the code below.

<?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>spring-4-mvc-hello-world-annotation</display-name>
    
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

/src/main/webapp/WEB-INF/web.xml

 

Step 4. Create spring-servlet.xml file.

On the same folder where "web.xml" is located, create another xml file, name it "spring-servlet.xml". Copy the following code into that file.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <context:component-scan base-package="com.consistentcoder.controllers" />
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

/src/main/webapp/WEB-INF/spring-servlet.xml

It is named "spring-servlet.xml" because the DispatcherServlet name that was defined on "web.xml" file is "spring". And the default format of the servlet xml file is [servlet-name]-servlet.xml.

 

Step 5. Create the template file.

Inside the folder "WEB-INF", create a new folder and name it "jsp". Then inside the newly created "jsp" folder, create a new jsp file that will contain the code below, name it "hello.jsp".

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC Hello World Annotation Example</title>
</head>
<body>
    <h1>${ message }</h1>
</body>
</html>

/src/main/webapp/WEB-INF/jsp/hello.jsp

 

Step 6. Create the Controller Class.

Create a new package, name it "com.consistentcoder.controllers". After that, create a new class inside that newly created package. Name the class "Main".

Tutorials:
Create a New Package on Eclipse IDE
Create a New JAVA Class on Eclipse IDE

package com.consistentcoder.controllers;

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

@Controller
public class Main {
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(ModelMap model) {
	
		model.addAttribute("message", "Spring 4 MVC Hello World Example Built with Maven");
		return "hello";
	}
}

/src/main/java/com/consistentcoder/constrollers/Main.java

 

That's all!

Spring 4 MVC Hello World Annotation built with Maven has been developed.

 

Final Project Structure

 

What's Next? Test the application.

Start your server and browse this link. http://localhost:8080/spring-4-mvc-hello-world-annotation/

 

 
 

Source Code