Spring MVC Hello World Example by R4R Team

Spring MVC Hello World Example:-

The following example show how to write a simple web based Hello World application using Spring MVC framework. To start with it, let us have working Eclipse IDE in place and follow the following steps to develope a Dynamic Web Application using Spring Web Framework:

1 Create a Dynamic Web Project with a name SpringMVC and create a package r4r.in under the src folder in the created project.

2 AddSpring and other libraries into the folder WebContent/WEB-INF/lib.

3 Create a Java class HelloWorldController under the r4r.in package.

4 Create Spring configuration files Web.xml and spring-servlet.xml under theWebContent/WEB-INF folder.

5 Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view file hello.jsp under this sub-folder.

6 The final step is to create the content of all the source and configuration files and export the application as explained below.

Following are the list of JAR files required for this application.

1 commons-logging-1.0.4.jar

2 jstl-1.2.jar

3 org.springframework.asm-3.0.1.RELEASE-A.jar

4 org.springframework.beans-3.0.1.RELEASE-A.jar

5 org.springframework.context-3.0.1.RELEASE-A.jar

6 org.springframework.core-3.0.1.RELEASE-A.jar

7 org.springframework.expression-3.0.1.RELEASE-A.jar

8 org.springframework.web.servlet-3.0.1.RELEASE-A.jar

9 org.springframework.web-3.0.1.RELEASE-A.jar

1 Now Here is the content of HelloWorldController.java file:

package r4r.in;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;


public class HelloWorldController {


public ModelAndView helloWorld() {

String message = "Hello Spring MVC Example";

return new ModelAndView("hello", "message", message);


2 Following is the content of Spring Web configuration file web.xml.

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"



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">

















3 Following is the content of another Spring Web configuration file spring-servlet.xml.

<?xml version="1.0" encoding="UTF-8"?>

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









base-package="r4r.in" />

<bean id="viewResolver"


<property name="viewClass"

value="org.springframework.web.servlet.view.JstlView" />

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

<property name="suffix" value=".jsp" />



4 Following is the content of Spring view file hello.jsp.



<title>Spring  MVC Series: Hello World - r4r.in</title>






5 Following is the content of Spring view file index.jsp.



<title>Spring  MVC Series: Index - r4r.in</title>



<a href="hello.html">Click Here</a>



6 Output:

Hello Spring MVC Example

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!