Spring MVC Hello World Example with Maven and JSTL

In this Spring MVC tutorial, we are building hello world application using Spring MVC framework. Follow the given instructions step by step and learn the basics. We have used JSTL for writing the view files.

1. Development environment

  1. Maven
  2. Eclipse juno
  3. Tomcat 7

2. Create Spring MVC Application

2.1. Create maven web application

To create a maven web application, open command prompt and make current working directory into eclipse workspace.

Now execute below command.

$ mvn archetype:generate -DgroupId=com.howtodoinjava.mvc 

Please remove the line break from above command. I added it for making it readable.

Now run below given commands. This will convert the created project into eclipse web project.

$ cd firstSpringApplication

$ mvn eclipse:eclipse -Dwtpversion=2.0

2.2. Update maven dependencies

Update the pom.xml file with these dependencies.


<!-- Spring 3 dependencies -->





3. Create Dispatcher Servlet

3.1. Update web.xml

Update web.xml for Servlet mapping and spring configuration location.

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


3.2. Bean configuration file

We will need to write configuration file (spring-mvc-servlet.xml) inside WEB-INF folder. We can name it at our will, but keep remember that it must match the servlet name we declared in web.xml.

<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="com.howtodoinjava.web" />

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


3. Create Spring MVC Controller

This request handler will receive all requests to a particular URL and return the model data to view handler configured.

package com.howtodoinjava.web;

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

public class DemoController
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model)
		model.addAttribute("message", "Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com");
		return "helloWorld";

4. Create Views

Our view is a jsp file, which will be rendered in browser when related view will be returned from server.

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>

		<title>My first example using Spring 3 MVC</title>
		<h1>Welcome message : <c:out value="${message}"></c:out></h1>

5. Demo

To run the application, configure tomcat server in eclipse. Now, right click on project and choose run as. On sub menu, choose Run on server.

Now type on browser: http://localhost:8080/firstSpringApplication/access

We should be able to see the message “Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com” in browser.

If you find any problem in running or deploying given application, drop me a comment.

Happy Learning !!

Oldest Most Voted
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.