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

eclipse-project

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.

spring-jars

Step 3: Create Controller Class

HelloWorldController.java

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;

@Controller
@RequestMapping("/helloworld")
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"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

Step 5: Create Web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID" version="2.5">
  <display-name>Spring MVC Application</display-name>
  <servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

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

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

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

Comments

comments

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.

Comments

  1. Whats the difference between IOC & DI in Spring?

    • 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.

      Thanks,
      Krishna

  2. Prashant says:

    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?

Speak Your Mind

*