Spring 3 MVC Hello World Example

The below example will show you how to write a simple web based Hello World application using Spring MVC framework. Create a Dynamic Web Application in Eclipse IDE for our Spring Web Application.

Folder Structure

FolderStructure_Spring

Required Files

  • Index.jsp
  • MVC_Controller.java
  • web.xml
  • SpringConfig-servlet.xml
  • Welcome.jsp

Index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <a href="HelloWorld.html">Click here</a>
</body>
</html>

MVC_Contoller.java

package com.javainterviewpoint;

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

@Controller
public class MVC_Controller 
{
 @RequestMapping("/HelloWorld")
 public ModelAndView Welcome()
 {
 String message = "Welcome to JavaInterviewPoint Spring 3 MVC Tutorial";
 return new ModelAndView("Welcome","message",message);
 }
}

Web.xml

<?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/j2ee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee%20http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<servlet>
  <servlet-name>SpringConfig</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>SpringConfig</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

SpringConfig-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package="com.javainterviewpoint"/>

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/Jsp/"></property>
  <property name="suffix" value=".jsp"></property>
</bean>

</beans>

Welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

How it works?

  •  Run our application (index.jsp) and click on the “Click here” link.
  • Once you click on the link, controll will be passed to the DispatcherServlet mentioned in the web.xml
<servlet-name>SpringConfig</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  • Then the DispatcherServlet passes the controll to the  MVC_Controller.java(@Controller) . As it is annotated with @Controller Annotation in SpringConfig-servlet.xml.
  • The action “HelloWorld.html” be mapped in the controller, the controller verifies it under the @RequestMapping annotation.
  • Now Welcome() method will be called, it simply returns the ModelAndView Object with the three parameter.
    • 1st Parameter – View Name(Welcome)
    • 2nd Parameter -Key to access the message
    • 3rd Parameter – Value
return new ModelAndView("Welcome","message",message);
  • Through the ViewResolver (InternalResourceViewResolver) mapped in the SpringConfig-servlet.xml , the Welcome.jsp page will be resolved and rendered as the output.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/Jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

Important Note:

In the web.xml we have given the servlet-name as “SpringConfig” , so our configuration name should be “SpringConfig-servlet.xml”, in general remember configuration name should be like this [ <<servlet-name in web.xml>>-servlet.xml]

Leave a Reply

Your email address will not be published. Required fields are marked *