Spring 3.2 MVC Hello World Example

In this section, you will learn about Hello World example in Spring 3.2.


Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller

Spring 3.2 MVC Hello World Example

In this section, you will learn about Hello World example in Spring 3.2.

Spring 3.2 is a open source application framework based on Java platform. Spring MVC is the Spring's web framework.
Given below a basic tutorial for displaying a "hello world" message in Spring 3.2. This will help you understanding the basic configuration for Spring MVC :

The project structure of this example is given below :

The list of the jar file used in this example is given below :

  • commons-logging-1.1.1.jar

  • jstl-1.2.jar

  • spring-beans-3.2.0.RELEASE.jar

  • spring-context-3.2.0.RELEASE.jar

  • spring-core-3.2.0.RELEASE.jar

  • spring-expression-3.2.0.RELEASE.jar

  • spring-web-3.2.0.RELEASE.jar

  • spring-webmvc-3.2.0.RELEASE.jar

First you need to configure web.xml  as follows :

web.xml( Spring3.2HelloWorld/WebContent/WEB-INF/web.xml )

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 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_3_0.xsd"


As you can see the welcome file is "index.jsp", it means it will be the first page, which will show to you. The DispatcherServlet is configured  with <servlet-name> "dispatcher". You can choose any name for <servlet-name>. Once it will initialize, it will search for a configuration file   <servlet-name>-servlet.xml or dispatcher-servlet.xml in WEB-INF folder of web application. This fill is necessary to configure your MVC application. The dispatcher-servlet.xml used in this example is given below :

dispatcher-servlet.xml(  Spring3.2HelloWorld/WebContent/WEB-INF/dispatcher-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"
	<context:component-scan base-package="net.roseindia.spring3point2.controller" />
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
		value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />

The <context:component-scan> tag scans the classpath for annotated components that will be auto-registered as Spring beans. UrlBasedViewResolver resolver is configured to resolve the views.

The first page that will appear to you is "index.jsp", its code is given below :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index or Home Page</title>
<a href="welcome/hello.html"><i>Click here to print message</i></a>

All the request having URL pattern "*.html" is handled by the controller HelloWorldController.java, the code of this controller is given below :


package net.roseindia.spring3point2.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

public class HelloWorldController {
	public String sayHello(ModelMap model) {
		model.addAttribute("message", "Spring 3.2 MVC Hello World");
		return "hello";	

In the above controller, you can see that the request through URL "/welcome/hello.html" is handled by the method sayHello(). This method will return string "hello" which is resolved by the view resolver configured in dispatcher-servlet.xml and it will look for hello.jsp file in "/WEB-INF/jsp/hello.jsp".

The code for the hello.jsp is given below :


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.2 Hello World</title>


When you execute your application, you will get the following page first :

When you click on the hyperlink, you will get the following page with message :

You can download the source code without jar file using below link.

Download Source Code


Share on Google+Share on Google+

Spring 3.2 MVC Hello World Example

Posted on: February 7, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Spring 3.2 MVC Hello World Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 12, 2013
Hello World