Spring MVC Example

This tutorial explains simple Hello World Example for start  learning Spring MVC. We have published several tutorials on advanced concepts using spring framework, including spring mvc examples. However, this post is just re-post for the beginners just want to write a bare minimum code for setting up the spring mvc project. If you have any questions on running this example, please write it in the comments section.

Step 1 : Create Dynamic Web Project in Eclipse


Step 2: Import or Copy the required JAR files to the eclipse project or keep it in the class path of your project build. If you are using the maven build, specify the appropriate artifacts in the POM.xml.


Step 3: Create Controller Class


package javabeat.net;

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 HelloWorldController{
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello World Spring MVC Framework!");
      return "hello";

Step 4: Create Spring MVC Configuration File HelloWorld-servlet.xml

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

   <context:component-scan base-package="javabeat.net" />

      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />

Step 5: Create Web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="WebApp_ID" version="2.5">
  <display-name>Spring MVC Application</display-name>

Step 6: Create JSP File (View) hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<title>Hello World</title>

Once you have created all the required files, your project structure would look like this:
spring-mvc-hello-world-1 Add this project to server and run the application. You can access it from the browser. spring-mvc-hello-world-2



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Siddu

    Whats the difference between IOC & DI in Spring?

    • http://www.javabeat.net Krishna Srinivasan

      HI Siddu,

      We can not compare IOC and DI. IOC is a design principle and DI is the concrete implementation for using the IOC.

      Coming to Spring framework, IoC and DI means the same thing. Spring uses Dependency Injection (DI) design pattern to achieve the IoC principle. It is one of the technique, there could numerous way you can use IoC.

      I hope this clears your doubts.


  • Prashant

    Hi Krishna,

    Could you please throw some details on ApplicationContext? Like, what is it? what is the use? I refereed several links on Net but not getting clear idea on this. Could you please explain it?