Getting started with Spring MVC

Let me introduce you to setup a very basic Spring 4 web application. I will demonstrate it in step-by-step:

  • Step 1 : We will use maven as build tool for our project. I have 3.3.9 installed in my machine and Java version is 1.8. To create a web template, type this in command line:

mvn archetype:generate -DgroupId=com.naveenlabs -DartifactId=SimpleWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

  • Step 2 : Now, go inside the folder SimpleWebApp, and edit pom.xml file. I imported the project into IntelliJ Idea. We will use light weight Jetty web server plugin as well. Update your pom file to this,
<project xmlns="" xmlns:xsi=""
  <name>SimpleWebApp Maven Webapp</name>


    <!-- JSTL for views -->
    <!-- compile only, runtime container will provide this -->
    <!-- Need this for config annotation -->


  • Step 3 : Now, run the command :
    mvn dependency:resolve
  • Step 4 : After all the dependencies are there, you can run the command :
    mvn jetty:run

    The above command will allow you to test if the web application and web server is running fine on your local system. If the web server starts properly, then it will show something like this on command line


Now, the above screen shows us that if we go to http://localhost:8080/myapp, it will show the default Hello World. So, that will make sure that our initial setup is alright. Let us move to make this spring application.

  • Step 5 : At this moment we need to add few files and folders to the existing app, such as java/com/naveenlabs/controllers etc. , and the whole folder structure should look like this, ( you can compare the folders and files and just add it to your app):


  • Step 6 : Let us edit our web.xml and update it with this:
<web-app xmlns=""

  <display-name>Counter Web Application</display-name>





  • Step 7 : Edit your spring-mvc-servlet.xml file and update it with this contents:

<beans xmlns=""

<context:component-scan base-package=”com.naveenlabs.controllers” />

<property name=”prefix”>
<property name=”suffix”>


  • Step 8 : In your, edit it with the following content:

package com.naveenlabs.controllers;

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

public class HomeController {

private static int counter = 0;
private static final String VIEWNAME = “home”;

@RequestMapping(value = “/”, method = RequestMethod.GET)
public String welcome(ModelMap model) {

model.addAttribute(“message”, “Welcome”);
model.addAttribute(“counter”, ++counter);
// logger.debug(“[welcome] counter : {}”, counter);

// Spring uses InternalResourceViewResolver and return back index.jsp
return VIEWNAME;


@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
public String welcomeName(@PathVariable String name, ModelMap model) {

model.addAttribute(“message”, “Welcome ” + name);
model.addAttribute(“counter”, ++counter);
// logger.debug(“[welcomeName] counter : {}”, counter);
return VIEWNAME;




  • Step 9 : And, finally your home.jsp should be :
<h1>Maven + Spring MVC Web Project Example</h1>

<h2>Message : ${message}</h2>
<h2>Counter : ${counter}</h2>
  • Step 10 :  Do,  mvn package and then mvn jetty:run to run the application, Now, if you go to http://localhost:8080/myapp/, it will show :