Spring MVC 4 - Hello world example

Posted on March 18, 2017

In this post, we will show you how to create a simple Hello World Spring MVC application using the Maven build tool and Eclipse IDE.

Tools and technologies used-

  • Spring 4.3.7.RELEASE
  • Eclipse Mars.2 (4.5.2)
  • Maven 3.3.9
  • JavaSE 1.8
  • Apache Tomcat  7.0.47 (Embedded)

Spring MVC 5 - Hello world example with Thymeleaf template

So let us proceed to write a simple Spring MVC application which will print “Hello Spring MVC”.

Step 1 - Maven dynamic web project

Create a maven web project for our Spring MVC hello world application.

You can refer this article to learn - how to create a maven web application in Eclipse IDE.

Step 2 - Project structure

Review the following web project structure.


Step 3 - Jar dependencies

Open your pom.xml file and write the following code in it.

<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">

    <!-- Spring MVC Dependency -->

    <!-- JSTL Dependency-->
    <!-- Servlet Dependency -->
    <!-- JSP Dependency -->


      <!-- Embedded Apache Tomcat required for testing war -->


Step 4 - Spring configuration

Create a web @Configuration class annotated with @EnableWebMvc and @ComponentScan as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

 * @author imssbora

@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

   public InternalResourceViewResolver resolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      return resolver;


The @EnableWebMvc enables default Spring MVC configuration and provides the functionality equivalent to <mvc:annotation-driven/> element in XML based configuration.

The @ComponentScan scans the stereotype annotations (@Controller, @Service etc...) in a package specified by basePackages attribute.

Similarly create a root @Configuration class as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Configuration;

 * @author imssbora
public class RootConfig {
   //Service and Repository beans configuration

Step 5 - Servlet container initialization

 With the release of Servlet 3.0, we can initialize the Servlet container without web.xml.

Create a container initializer class by extending the AbstractAnnotationConfigDispatcherServletInitializer class as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 * @author imssbora
public class MyWebAppInitializer extends 

   protected Class<?>[] getRootConfigClasses() {
      return new Class[]{RootConfig.class};

   protected Class<?>[] getServletConfigClasses() {
      return new Class[]{WebConfig.class};

   protected String[] getServletMappings() {
      return new String[]{"/"};

The AbstractAnnotationConfigDispatcherServletInitializer class, implements the WebApplicationInitializer, is implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically.

Step 6 - Controller

Create a @Controller class under com.boraji.tutorial.spring.controller package and write the following code in it.


package com.boraji.tutorial.spring.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

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

 * @author imssbora
public class HelloWorldController {
   public String sayHello(Model model) {
      model.addAttribute("message","Hello Spring MVC!");
      //Java 8 LocalDate
      DateTimeFormatter formatter=DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
      LocalDate date=LocalDate.now();
      model.addAttribute("date", date.format(formatter));
      return "index";

Step 7 - View

Create an index.jsp file under src/main/webapp/WEB-INF/views folder and write the following code in it.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - Hello World Example | BORAJI.COM</title>
	<h4>Server date time is : ${date} </h4>


Step 8- Build project + Run embedded Tomcat

Use the following maven command to build project.

mvn clean install

Refer this link to run the above command in Eclipse IDE.

Use the following maven command to run embedded tomcat server.

mvn tomcat7:run

Refer this link to run the above command in Eclipse IDE.

Step 9- Test web project

Open browser and type http://localhost:8080/ URL in address bar.

Spring MVC Hello World Example - BORAJI.com

Download Sources