In the course of this tutorial, we will learn how to create your first Spring MVC application using eclipse IDE using Maven build type.

1. Prerequisites

  • Java version 1.7
  • Latest Spring STS
  • Apache Tomcat 7.0.x

This tutorial assumes that, you have downloaded and installed the above mentioned softwares on your computer. If you have not installed Java on your development machine, you can follow the official oracle documentation for the installation for your configuration.

Without spending any further time, let us jump straight into creating the project in eclipse.

1. Create a Maven Project

  • File -> New -> Other… It will open the eclipse select wizard.

Create Maven Spring project

  • Select Maven -> Maven Project and click Next.
  • Again click Next on New Maven Project dialog.
  • Select an Archetype either by filtering webapp or selecting from the available artifacts and click Next.

Create new eclipse maven project

  • Provide the details for Group Id and Artifact Id and click Finish. As a general practice, the group id is your company domain name (i.e. com.javatechig) and artifact id is the name of your application.
  • This will create a basic Maven project template in eclipse.

3. Create a Server Instance

In this example we will be using Apache Tomcat application server for deploying our Spring MVC application. Now let us add a server instance on eclipse.

  • File -> New -> Other… It will open the eclipse select wizard.

Add Tomcat Server Eclipse

  • Select Server -> Server and click Next.
  • Select Apache -> Tomcat v7.0 Server and click Next. In this example, we will be using Apache Tomcat version 7.0. However, the similar steps will work for other tomcat versions.
  • Browse and select the Apache Tomcat v7.0 server installation directory and click Finish.

4. Update Project Build Path

You might notice a error on your project. This is due to the project Build path problem. To fix this you need to right click on project -> Properties to open project Java Build Path settings.

Select Libraries tab and click on Add Library…-> Server Runtime -> Apache Tomcat -> Finish.

5. Configure Spring Dependency

Before we develop Spring MVC web application, we need to configure the project by adding the required Maven dependencies. Add the following Maven dependencies into your pom.xml file.

	<name>HelloMVC Maven Webapp</name>

	<!--Spring library version -->


		<!-- Spring dependencies -->


		<!--End Spring dependencies -->


With this the basic Spring project configuration is complete. Your project structure should look like as the following screenshot.

Spring MVC Eclipse Maven Project Structure

The following steps will take you through the rest of the steps to create and deploy Spring MVC HelloWorld app.

6. Configure Dispatcher Servlet

The DispatcherServlet must be configured as normal in web.xml to bootstrap a Spring WebApplicationContext. Edit the default web.xml file and add the following.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

	<display-name>Archetype Created Web Application</display-name>




7. Mapping Requests

Create an xml file dispatcher-servlet.xml under the same directory of web.xml.

<beans xmlns=""

	<context:component-scan base-package="com.javatechig.controller" />

	<bean		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
		<property name="suffix">

In the above xml file, base-package specifies the package of the controllers. prefix specifies the directory of views, and it is set to be /WEB-INF/views/, which means views directory should be created under WEB-INF. suffix specifies the file extension of views. For example, given a view hello, the view will be located as /WEB-INF/views/hello.jsp.

8. Create Controller class

Create the HelloWorldController under src/main/java/ directory.
package com.javatechig.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

public class HelloWorldController {

	public ModelAndView welcomeMessage(
			@RequestParam(value = "name", required = false) String name) {
		// Name of your jsp file as parameter
		ModelAndView view = new ModelAndView("hello");
		view.addObject("name", name);
		return view;

In the code above, @RequestMapping annotation maps web requests onto specific handler classes and/or handler methods, in this case, welcomeMessage(). It provides a consistent style between Servlet environments, with the semantics adapting to the concrete environment.

RequestParam indicates that a method parameter should be bound to a web request parameter. In this case, we also make it not required and give it a default value. The ModelAndView(“hello”) determines that hello is the target view.

9. Working with Views

Edit the default index.jsp and add the following code snippets.

    <h2>Hello World</h2>
    <h3><a href="hello?name=Sally">Click here...</a></h3>

Add a JSP files hello.jsp file under /WEB-INF/views/ directory.

<%@ page language="java" contentType="text/html; charset=UTF-8"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC</title>
<%@ page isELIgnored="false"%>
	<h2>Hello, ${name}. Welcome to Spring MVC!</h2>

Now we are done with our first HelloWorld example. To deploy it on Tomcat application server, Right click on the project -> select Run as. Choose Tomcat server, select Next and Finish. It will deploy the project and to see the output visit http://localhost:8080/HelloMVC on your browser. you will see the following output on your browser.

Spring MVC HelloWorld usign Eclipse Maven2

Spring MVC HelloWorld usign Eclipse Maven

This article is about: MVC, Tomcat,


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (, DZone MVB and founder of


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.