Basic Spring MVC Application

This post explains how to create spring based web application.

Folder Structure:

Typical folder structure of the spring web application contains as below.

Folder Structure


Below is the web.xml file which contains the spring configuration. DispatcherServlet is controller of the spring mvc application, all the requests which coming to the application will be passed through it.  DispatcherServlet loads the spring-context.xml file during server start up and keeps the all the configurations in servlet context. Action path configured for this applications is *.html.

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”;
id=”WebApp_ID” version=”2.5″>

<display-name>Spring Hello World</display-name>




index.jsp file configured in web.xml as a welcome file, so this file will be loaded when user hit the http://localhost:8080/SpringMVC/ url in the browser.

<jsp:forward page=”sayhello.html”></jsp:forward>

When ever index.jsp loaded into the browser, it will forward the request to sayhello.html. This sayhello request is mapped to sayHello() method in HelloWorldController class. So, sayHello() method will be executed and it will return the ‘hello’ string after completion of method execution.

package in.javatutorials.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

* @author Javatutorials
public class HelloWorldController {

* Controls the ‘sayhello’ and return to hello.jsp
* @return
@RequestMapping(value = “/sayhello”, method = RequestMethod.GET)
public String sayHello() {

System.out.println(“Sample Spring Application”);

return “hello”;



Package of HelloWorldController  class is configured as a controller package in the spring-context file as mentioned below. This sample application is configured with the InternalResourceViewResolver, when ever sayHello() method returns the hello string, request will be redirected to hello.jsp based upon the InternalResourceViewResolver configuration as mentioned in the below file.

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”;
xmlns:xsi=”; xmlns:mvc=”;

<context:component-scan base-package=”in.javatutorials.spring.controller” />
<mvc:annotation-driven />

<property name=”prefix” value=”/jsp/” />
<property name=”suffix” value=”.jsp” />



Finally ‘Hi, This is Sample Spring MVC Application’ message will be shown to the user as it is mentioned in the below jsp.

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”&gt;
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Sample Spring MVC Application</title>
<body>Hi, This is Sample Spring MVC Application

Access the application in Browser:


Application Browser

Please click on the below link to download the complete application : Click here

Other Useful links:

Spring MVC with Tiles framework sample application

differences between DispatchAction and LookupDispatchAction in Struts Framework

Create a Java web service using top down approch

Example programs for SOAP webservice client

2 thoughts on “Basic Spring MVC Application

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.