Spring 5 MVC Hello World using Annotation

In the previous tutorial, we have created a simple Spring MVC hello world example by using XML configuration. But in this tutorial, we will create the same application using annotation. To do that the Spring framework provides @Controller and @RequestMapping annotation.

Annotation reduced the complexity of the application and most of the developer doesn’t like to manage XML files.

@Controller: Indicates that an annotated class is a Controller.
@RequestMapping: Annotation for mapping web requests onto specific handler classes and/or handler methods.
@RequestParam: Annotation which indicates that a method parameter should be bound to a web request parameter.

In the next tutorial, we show you how to create Java-based Spring MVC configuration. In this case, we will not create a single XML file.

Technology Used

Find the list of all technologies that I have used in this application.

  • Eclipse Photon IDE
  • JDK 8
  • Tomcat 9
  • Spring.5.0.2. RELEASE

Dependency Required

To create Spring MVC application, you must need Spring MVC jars in your project build path and Maven is a great tool to do that. Add the following dependency to your pom.xml file.


Project Structure

Final project structure of our application in Eclipse IDE will look like as follows.

Spring 5 MVC Hello World using Annotation

Configure Front Controller

Add the Spring MVC front controller i.e. DispatcherServlet to your web.xml file.

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

Create Controller

Create a HelloController class and add the @Controller annotation on the top of the class. And to handle the action request create a greet(name) method and add @RequestMapping annotation on the top of the method.

package org.websparrow;

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

public class HelloController {

	public ModelAndView greet(@RequestParam("name") String name) throws Exception {

		ModelAndView mv = new ModelAndView();

		// name of JSP page

		// data that you want to show on JSP in form of key and value
		mv.addObject("name", name);

		return mv;


Scan Packages

We have mapped action request to the controller using annotation so there is no need to map here but we need to tell front controller where is my controller. In this XML file, we only need to scan the packages.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="org.websparrow"></context:component-scan>

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


View Components

Create the JSP pages that visible on the user machine.

index.jsp: This page will take the username and sends a request to the controller.


	<h2>Spring 5 MVC Hello World using Annotation</h2>

	<form action="hello.htm" method="post">

		Name: <input name="name" type="text" />
		      <input value="Submit" type="submit" />


welcome.jsp: This page will greet the user.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>

	<h2>Hello ${name}</h2>
	<p>Welcome to Spring MVC world.</p>


Run Application

Finally, run your application and will get the following result.

Spring 5 MVC Hello World using Annotation


  1. Spring Web MVC
  2. Front Controller- Dispatcher Servlet

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.