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.

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"> <modelVersion>4.0.0</modelVersion> <groupId>com.howtodoinjava</groupId> <artifactId>spring-boot-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-demo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat Embed --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- To compile JSP files --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> </project>
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; @SpringBootApplication public class SpringBootWebApplication extends SpringBootServletInitializer { @Override 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; @Controller public class IndexController { @RequestMapping("/") public String home(Map<String, Object> model) { model.put("message", "HowToDoInJava Reader !!"); return "index"; } @RequestMapping("/next") 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
spring.mvc.view.prefix=/WEB-INF/view/ spring.mvc.view.suffix=.jsp //For detailed logging during development logging.level.org.springframework=TRACE logging.level.com=TRACE
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; @Configuration @EnableWebMvc @ComponentScan public class MvcConfiguration extends WebMvcConfigurerAdapter { @Override public void configureViewResolvers(ViewResolverRegistry registry) { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/view/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); registry.viewResolver(resolver); } }
JSP Files
Two used JSP files in this spring boot jsp example – are below.
index.jsp
<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html lang="en"> <body> <div> <div> <h1>Spring Boot JSP Example</h1> <h2>Hello ${message}</h2> Click on this <strong><a href="next">link</a></strong> to visit another page. </div> </div> </body> </html>
next.jsp
<!DOCTYPE html> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <html lang="en"> <body> <div> <div> <h1>Another page</h1> <h2>Hello ${message}</h2> Click on this <strong><a href="/">link</a></strong> to visit previous page. </div> </div> </body> </html>
Demo
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/

Click next link

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 !!
References:
Spring Boot
Spring Boot Application Properties
Spring MVC
is it possible to run .html with internal resolver
HI,
Using src/main/webapp/WEB-INF/jsp/welcome.jsp is discouraged if you want to deploy via a jar package.
“Do not use the src/main/webapp folder if your application will be packaged as a jar. Although this folder is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.”
Reference: https://docs.spring.io/spring-boot/docs/1.1.4.RELEASE/reference/htmlsingle/#using-boot-structuring-your-code
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Jan 26 12:22:18 IST 2020
There was an unexpected error (type=Internal Server Error, status=500).
The absolute uri: http://www.springframework.org/tags cannot be resolved in either web.xml or the jar files deployed with this application
Worth mentioning, this project as a standalone would not work as Versions of dependency items are missing in pom.xml. After a little research I found it is depending upon a ‘spring-boot-starter-parent’ project as baseline. My guess is, version of dependencies are being controlled from the parent project. As Ankit has mentioned above, we need a tool to download the dependencies from parent project i.e.
mvn eclipse:eclipse -DwtpVersion=2.0
command.org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
Thank you it’s working on my machine
for all those who don’t able to import this in your STS .plz try the below steps.
1) unzip the file and copy the unzipped file to your work-space(i.e the drive) u have chosen for STS.
2)open the unzipped folder (normally in windows) and got to POM file. Now click on the address bar .and copy it.
ur address bar should look something like this.
D:\SPRING BOOT 4\WORKSPACE SPRING BOOT\spring-boot-demo-jsp-example\spring-boot-demo
note: the address at last must be showing \spring-boot-demo
3)now open cmd and browse to the specific drive (if it is other than C drive) abd paste the copied address and press enter
4) NOw the important part … Type the command Exactly like I have written.
mvn eclipse:eclipse -DwtpVersion=2.0
and press enter if everything is right it will start downloading the required files and Build Success will be shown.
Now just open ur STS and try to import the file u will be able to import it. 🙂
Thanks for sharing it.
As a fix ,in pom.xml, for spring-boot-starter-tomcat remove tag. It is needed only if its not a spring boot application. It is failing because there is no explicit tomcat server.
After fixing this issue also example is not working properly.
Suppose I am hiiting localhost:8080/next
then response is
2019-02-14 16:29:20.601 DEBUG 2436 — [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/view/next.jsp
2019-02-14 16:29:20.602 DEBUG 2436 — [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/view/next.jsp]
basically is not treating it a request to get static resource.
If you create a method in controller with @Requestparam(/WEB-INF/view/next.jsp) then it gets detected.
Lokesh could you help us on this.
I cannot get this to work. I downloaded your source and imported it as a project into Intellij Idea. When I try to run it, I get the following exception:
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized – call ‘refresh’ before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@1e4a7dd4: startup date [Tue Jul 10 21:12:36 PDT 2018]; root of context hierarchy
Try this: https://stackoverflow.com/questions/44686432/java-lang-illegalstateexception-applicationeventmulticaster-not-initialized-c/45450371#45450371
I downloaded the attached example and imported in eclipse as maven project and it worked perfectly.
Thanks
When I run this code using “mvn spring-boot:run”, then application is running fine. But when I converted the same application into executable jar file, (changed war to jar in pom.xml) and executed the jar file then this error is coming,
Could you please resolve this issue. I need this very urgent, in my project as well I’m encountering the same issue. Here is email id, “tshravan2012@gmail.com”. Thanks a lot for your time.
Hmmm, mvn spring-boot:run works while idea spring-boot doesn’t. This is a problem
Your example doesn’t work.
I encountered next error: “.. Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter”
which shows me that it caused by explicit dependency for tomcat with provided: https://stackoverflow.com/questions/30374316/spring-boot-java-lang-noclassdeffounderror-javax-servlet-filter
I wonder why should i have that dependency at all if spring uses tomcat embedded by default. So i removed it, but now i obtained: “Whitelabel Error Page”
you must run the app with mvn clean spring-boot:run command
I have followed you tutorial to add view layer to my existing spring boot rest application. Following is the method i am trying to use a home page view(home.jsp). I am not getting the jsp page render, instead I am getting a string ‘home’ when i ran the project. Please help me in fixing issue, I am new to spring boot.
@RequestMapping(value = “/dashboard”)
@ResponseBody
public String getMyMessage() {
return “home”;
}
my MvcConfiguration class is:
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;
@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(“/WEB-INF/views/”);
resolver.setSuffix(“.jsp”);
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
}
}
@Neelam Panwar, You need to remove @ResponseBody annotation from your getMyMessage() method. That is for render a JSON object or a string. If you want to return a JSP view. you just need to remove that.