Saturday, October 27, 2012

Spring MVC Hello World Example

In this post, we are going to see how to implement Spring MVC HelloWorld example.
Step 1:
The initial step is to create Dynamic Web Project in eclipse. To create a new Dynamic project in Eclipse IDE select File -> Dynamic Web Project.
Step 2:
After that New Dynamic Project Window will appear on the screen and you will enter the web application name in the Project name text box. Enter SpringHelloWorld in the Project Name and then click Next button.

Step 3:
Add Spring Web MVC jar files.
Step 4:
Now we will created a index.jsp in project's WebContent  folder. In the jsp file we will create a new hyperlink "Click Here" that will be linked to page.  The code of index.jsp is:

<title>Spring Demo</title>
<a href="">Click Here</a>
Step 5:
Now we will configure the DispatcherServlet in web.xml file. The DispatcherServlet will be configured to process all the request ending with *.do
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="" xmlns:xsi=""
<description>Hello world Application</description>
Step 6:
Now we will create a new xml file called controller-servlet.xml in the WEB-INF folder of the web application. This is the main configuration file for the Spring MVC.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
Then we will define the url mapping with the Controller bean as shown below:

<property name="mappings">
<prop key="/">helloController</prop>
Finally we will define the Controller bean as shown below:
<bean id="helloController" class="com.ranga.HelloWorld"> </bean>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<bean id="urlMapping"
<property name="mappings">
<prop key="/">helloController</prop>
<bean id="helloController" class="com.ranga.HelloWorld" />
Step 7:
After that we will creates the controller class in the project src folder. This will be used as the controller for the hello.jsp request. The code of the calss is:

package com.ranga;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorld implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
String str = "Hello World!";
return new ModelAndView("hello.jsp", "message", str);
Step 8:
After that we will create a hello.jsp in the WEB-INF folder. The WEB-INF/hello.jsp code is:

<%@page isELIgnored="false" %>
Step 9:
To run the example select Run -> Run As -> Run On Server from the menu and then run the application on tomcat server. Eclipse will start Tomcat server and deploy the application on the Tomcat. Ellipse will also open the application in it internal browser as shown below:
Click on the "Click Here" link to test the application. It should display the "Hello World!" message as shown below.