By using this tutorial you will easily understand how to develop a Spring 4 MVC example with Maven. I have developed this spring hello world application according to annotation based and step by step process, beginners can easily understand this tutorial. We can also do the same example without maven but you have to download and add required jars manually. Spring is a famous framework using spring we can build complicated applications. Spring mvc is a web part of spring framework build around the principles of spring and it support for Themes, Locales/i18n, Restful services, Annotation based configuration and Seamless integration with other Spring Services/Beans.

Tools used for this application:
  • Spring 4.0.4.RELEASE
  • JDK 1.6
  • Maven 3
  • Jstl
  • Spring Tool Suite (Version: 3.2.0.RELEASE): It is an open source eclipse flavoured IDE like juno, indigo, etc. Spring Tool Suite is also called as STS and it improves your Spring development environment with tips, popup guides, Spring guides and wizards that will make your working experience simpler and quicker.
 Note: Maven will create the necessary directory structure required for your web project. We can also do this example without using maven but in real time projects most of them use maven so i did this example using maven.

Step 1: First you have to Setup Maven Project in Eclipse for that open the eclipse go to File menu and do as shown in below screenshots.

Select the Maven Project and click on the next button then the select project name and location wizard will appears use the default workspace location, click the next and continue as shown in below screenshots.

Enter the below values and click finish.
  • Group Id: com.studywithdemo
  • Artifact Id: Spring4Example
  • Version: 0.0.1-SNAPSHOT
  • Package: com.studywithdemo
Using Maven we need only three dependencies
List of dependencies:

Now double click on the pom.xml file after that select the dependencies and click on the add button and give the values as shown below.

      Group Id: org.springframework
      Artifact: spring-webmvc
      Version:  4.0.4.RELEASE
      Group Id: javax.servlet
      Artifact: jstl
      Version:  1.2
      Group Id: javax.servlet
      Artifact: servlet-api
      Version:  2.5

Now right click on the pom.xml file and select the open with->text editor. It looks like below format.

<project xmlns="" xmlns:xsi=""
  <name>Spring4Example Maven Webapp</name>

Spring Configuration Parts

Configuring the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns=""
       <display-name>Archetype Created Web Application</display-name>

Configuring the servlet-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       <context:component-scan base-package="com.studywithdemo.controller"/>
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/WEB-INF/jsp/"/>
             <property name="suffix" value=".jsp"/>
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
       p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

Creating the Controller

package com.studywithdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
public class SampleController {
       @RequestMapping(value ="/welcome")
       public String sayHello (Model model) {
             model.addAttribute("greeting", "Hello, Welcome to StudywithDemo !");
             return "hello";

Create JSP for View

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Example</title>

Finally Run the Application

Type the url: http://localhost:8080/Spring4Example/welcome.html



Post a Comment