Spring Boot JSP View Resolver Example

Learn to create and configure spring boot jsp view resolver which uses JSP template files to render view layer. This example uses embedded Tomcat server to run the application.

Sourcecode Structure

The files in this application are placed as given structure in image.

Spring Boot Application Structure
Spring Boot Application Structure

Maven dependencies – pom.xml

This application uses given below dependencies.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>spring-boot-demo Maven Webapp</name>
		<!-- Web -->
		<!-- Tomcat Embed -->
		<!-- JSTL -->
		<!-- To compile JSP files -->

Spring Boot Application Initializer

The first step in producing a deployable war file is to provide a SpringBootServletInitializer subclass and override its configure() method. This makes use of Spring Framework’s Servlet 3.0 support and allows you to configure your application when it’s launched by the servlet container.

package com.howtodoinjava.app.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class SpringBootWebApplication extends SpringBootServletInitializer {

	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootWebApplication.class);

	public static void main(String[] args) throws Exception {
		SpringApplication.run(SpringBootWebApplication.class, args);

Spring Controller

Controller classes can have methods mapped to specific URLs in the application. In given application, it has two views i.e. “/” and “/next”.

package com.howtodoinjava.app.controller;

import java.util.Map;

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

public class IndexController {

	public String home(Map<String, Object> model) {
		model.put("message", "HowToDoInJava Reader !!");
		return "index";
	public String next(Map<String, Object> model) {
		model.put("message", "You are in new page !!");
		return "next";


Spring Boot JSP ViewResolver Configuration

To resolve JSP files location, you can have two approaches.

1) Add entries in application.properties


//For detailed logging during development


2) Configure InternalResourceViewResolver to serve JSP pages

package com.howtodoinjava.app.controller;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

public class MvcConfiguration extends WebMvcConfigurerAdapter
	public void configureViewResolvers(ViewResolverRegistry registry) {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();

JSP Files

Two used JSP files in this spring boot jsp example – are below.


<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
			<h1>Spring Boot JSP Example</h1>
			<h2>Hello ${message}</h2>
			Click on this <strong><a href="next">link</a></strong> to visit another page.


<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html lang="en">
			<h1>Another page</h1>
			<h2>Hello ${message}</h2>
			Click on this <strong><a href="/">link</a></strong> to visit previous page.


After whole code is written and placed inside folders, run the application by executing main() method in SpringBootWebApplication class.

Now hit the URL: http://localhost:8080/

Spring Boot Application - index
Spring Boot Application – index

Click next link

Spring Boot Application - next
Spring Boot Application – next

Spring Boot JSP example Source Code

Download the sourcecode of this application with below ink.

In this tutorial, we learned Spring Boot JSP ViewResolver with example, among other possible multiple view resolvers available to use.

Happy Learning !!


Spring Boot
Spring Boot Application Properties
Spring MVC

Leave a Reply

Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial