Configure JSP views in InternalResourceViewResolver with Spring Boot JAR file

Important: recommend you to use JSP views with WAR file, JAR file will have many problems, one of which I have encountered is deploying JAR file using Docker Container… The following tutorial is only for someone who must use JSP views with JAR files in Spring Boot.

JSP files, normally used with InternalResourceViewResolver, have some limitations when used in Spring Boot applications. According to Spring Boot documentation https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.developing-web-applications.embedded-container.jsp-limitations the JSP file will only work with Tomcat and Jetty server and will only work with WAR files, not with JAR files. However, in fact, we also have a way to make it work with JAR files. In this tutorial, I will show you how to configure JSP views in InternalResourceViewResolver with Spring Boot JAR file.

I will create a new Spring Boot project:

 

and use InternalResourceViewResolver for this example.

I will add tomcat-embed-jasper dependency:

and a controller:

The view file home.jsp with the following content:

will no longer be created in the /src/main/webapp/WEB-INF/views directory, Spring Boot when building will not include this directory in the JAR file. You need to create it in the src/main/resources/META-INF/resources/WEB-INF/views directory as follows:

The src/main/resources/META-INF/resources/ directory will work like the src/main/webapp directory!

Now I will declare in application.properties as follows:

Result when running the application:

Add Comment