Sunday, 24 March 2013

Spring 3 MVC Hello World Example

In this tutorial, you will learn how to display "hello world" message in Spring 3.0 MVC

The step by step method will give you a clear idea : 

 Step1 : First of all, create a dynamic project in eclipse of name "Spring3_MVC_HelloWorld" as given below :

Step2 : Add the spring 3 jar files to the lib folder of WEB-INF. The added jar files are given below :

Step3 : Editing deployment descriptor(web.xml). The entry point of Spring MVC application is the Servlet define in deployment descriptor web.xml . Here, the dispatcher servlet(org.springframework.web.servlet.DispatcherServlet) is the entry point. This xml file maps the DispatcherServlet with url pattern *.html. The first file that will appear to you i.e. index.jsp should be defined as welcome file in the web.xml. The code of the web.xml file is given below :


One thing to note here is the name of servlet in tag in web.xml. Once the DispatcherServlet is initialized, it will looks for a file name [servlet-name]-servlet.xml in WEB-INF folder of web application. In this example, the framework will look for file called spring-servlet.xml. This xml file is known as configuration file. Configuration File Create a file Dispatcher-servlet.xml in the WEB-INF folder. Copy the following content in it :



The tag is used to load all the components from package com.controller and all its sub packages. This will load our HelloWorldController class. The bean viewResolver will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. The HelloWorldController class return a ModelAndView object with view name ?hello?. This will be resolved to path /WEB-INF/jsp/hello.jsp. Step 4: Creation of index.jsp file which is the entry point for our application. Create index.jsp in WEB-INF folder and copy the following code in it :

Spring 3 Hello World Example

Show Me  Hello

Step5 : Create the controller class. First create package "com.controller" and add the class in it and copy the following code in it :
package com.controller;

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

public class HelloWorldController 
 public ModelAndView helloWorld() {
  System.out.println("In Controller");
  String message = "Hello World, Spring 3.0!";
  return new ModelAndView("hello", "message", message);
When the Spring scans our package The annotation @Controller identify this class as the controller bean for processing request. The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. That includes /hello/* and /hello.html. This viewResolver bean in Dispatcher-servlet.xml will resolve the view and add prefix string /WEB-INF/jsp/ and suffix .jsp to the view in ModelAndView. Note that in our HelloWorldController class, we have return a ModelAndView object with view name ?hello?. This will be resolved to path /WEB-INF/jsp/hello.jsp . Step6 : The View in MVC. Create hello.jsp to display hello world message. This hello.jsp should be placed inside WEB-INF/jsp . Copy the content from the jsp file given below :

Spring 3 MVC Hello World Example


Output : When you execute the application :

Download Complete Project


Post a Comment

Related Posts Plugin for WordPress, Blogger...