Spring MVC Example using JSP

Hey guys in this article, you will learn about Spring MVC example by creating controller and JSP view template.

Read More:


  • Controller is a Java class which contains the special annotation called @Controller
  • @Controller internally uses the @Component annotation
  • @Controller indicates that the class is a Spring MVC controller
  • Controller class contains java methods just like any other java class but these methods are mapped to a web requests.
  • These methods are mapped to a web requests using @RequestMapping annotations
  • @RequestMapping annotation contains the name of the web request such as /home, /dashboard, /contact, /about
  • @RequestMapping supports all types of web requests such GET, POST etc.,
  • These methods will return the name of the view page (only the name).

Complete Example

Lets create a step by step maven project to understand the Spring MVC

Create a maven project

First you need to create a maven webapp project. You can check the below post to create new maven webapp project –

  • Create a maven webapp project

Add Maven Dependencies

Open pom.xml and add the following maven dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>springmvc Maven Webapp</name>


Configure Dispatcher Servlet

Open web.xml and add the following content

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	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 -->

Configure View Resolver

Create spring-mvc-dispatcherservlet.xml under webapp/WEB-INF and add the following content

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

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="in.bushansirgur.springmvc" />

<!-- 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" />


Create a Controller

Inside src/main/java create a package in.bushansirgur.springmvc.controller, under this create HomeController.java and add the following content

package in.bushansirgur.springmvc.controller;

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

public class HomeController {
	@RequestMapping(value = "/")
	public String showHomePage() {
		return "home-page";

Create a view template

Create home-page.jsp inside webapp/WEB-INF/view folder and add the following content

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
	<h1>Spring MVC Example</h1>
	<p>Spring MVC Controller Example</p>

Run the App

Start the tomcat server, navigate to the URL localhost:8080/springmvc/


That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply