I started a Spring Boot MVC project and realized that there are two folder within resources
. One is called templates
and the other static
. I really like this folder setup.
The problem is that I use JSP Templates for my views. I could not place a .jsp
template inside the templates
folder and got it to work.
What I needed to do is to create a webapp
folder on the same level as src
and resources
. Placing my JSP templates in there and then my views can be found.
What do I need to reconfigure to actually use my JSP templates within the templates
folder which lies within resources
?
Official information:
Resource handling:
Supported template engine
[...]
[..]
source
Spring boot JSP limitations
source
Technical change
Tell spring boot to from where to load the
JSP files
. Inapplication.properties
setsource
Sample spring boot with JSP
In case you do want to use
JSP
with spring boot here are two examples:https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-samples/spring-boot-sample-web-jsp
https://github.com/joakime/spring-boot-jsp-demo
According to the Maven documentation
src/main/resources
will end up inWEB-INF/classes
in the WAR.This does the trick for Spring Boot in your
application.properties
:If you prefer Java configuration this is the way to go:
Update with a full example
This example was based on Spring's initializer (Gradle project with "Web" dependency). I just added
apply plugin: 'war'
to thebuild.gradle
, added/changed the files below, built teh project withgradle war
and deployed it to my application server (Tomcat 8).This is the directory tree of this sample project:
ApplicationConfiguration.java: see above
DemoApplication.java:
DemoController.java:
index.jsp:
To summarize it, none of the suggested answers worked for me so far. Using a blank Spring boot starter project.
Somehow, something looks hardwired inside Spring or servlets so that JSP must be in
/webapp
(or a subfolder). Unlike default thymeleaf templates which are looked up in/resources/templates
.I tried all kind of changes, really a lot of different configurations, but wasn't able to modify that behavior. It just produced complexity and was unable to serve the JSPs anymore. So, bottom line, if you're using JSPs, just put them in
/webapp
. It also works by addding zero configuration using a controller like:@GetMapping("/foo") public String serveFoo() { return "relative-path-inside-webapp/foo.jsp"; }
On another note, by default, the
/webapp
folder will also be hidden in the Spring Toolsuite, so you'll have to manually configure it as a "source folder".