Signup/Sign In

Spring MVC Hello World Application

In this article, we are going to create a simple hello world application using the Spring MVC. We created a maven based Spring project that contains a controller, a view(JSP file), and some XML files. Our project looks like below.

Project Structure

Project Files Source Code:


It is a controller class file that uses @Controller annotation and returns an index JSP page as a view component of MVC.

package com.studytonight.controller;

import org.springframework.stereotype.Controller;

public class HelloController {
	public String hello() {
		return "index";

// spring-mvc-app-servlet.xml

This is a configuration file that contains base-package locator and InternalResourceViewResolver to fetch view by adding suffix and prefix to the view(JSP) file.

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

	<!-- Step 3: Add support for component scanning -->
	<context:component-scan base-package="com.studytonight.controller" />

	<!-- Step 4: Add support for conversion, formatting and validation support -->

	<!-- Step 5: Define Spring MVC view resolver -->
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />

// web.xml

This XML file is required when we work with Spring MVC and used to configure the front controller DispatcherServlet and application context file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.1">


	<absolute-ordering />

	<!-- Spring MVC Configs -->

	<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->

	<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->

// index.jsp

It is a view file that will be returned by the controller to the client.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Hello Spring</title>
<p>Hello Welcome to Spring MVC</p>


Run the application and you will get the output like below in your browser.

No XML Configuration of Spring MVC

In case, we want to configure the Dispatcher servelet using Java code only (No XML) then include the following files into you project and delete the spring-mvc-app-servlet.xml and web.xml file.

Project Structure

The config files has the following source code.


This class is similar to web.xml and used to configure DispatcherServlet for the Spring application. The getServletConfigClasses() method is used to load class.

package com.studytonight.config;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	protected Class<?>[] getRootConfigClasses() {
		// TODO Auto-generated method stub
		return null;

	protected Class<?>[] getServletConfigClasses() {
		return new Class[] { AppConfig.class };

	protected String[] getServletMappings() {
		return new String[] { "/" };


This is similar to the application context where we configure our views file. Here, we used InternalResourceViewResolver class and its methods setPrefix(), setSuffix() to configure the view file.

package com.studytonight.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
public class AppConfig {
	// define a bean for ViewResolver
	public ViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		return viewResolver;

Add these files to your project and run the project. It will produce similar output.

About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.