The Problem: Hot-Reload Of Thymeleaf-Templates Does Not Work, When The Application Is Run With spring-boot:run
A lot of people seem to have problems with hot reloading of static HTML-ressources when developing a Spring-Boot application that uses Thymeleaf as templateing engine with spring-boot:run.
There are a lot of tips out there, how to fix that problem:
- The official Hot-Swapping-Guide says, that you just have to add
spring.thymeleaf.cache=falsein your application-configuration insrc/main/resources/application.properties. - Some say, that you have to disable caching by setting
spring.template.cache=falseandspring.thymeleaf.cache=falseand/or run the application in debugging mode. - Others say, that you have to add a dependency to
org.springframework:springloadedto the configuration of thespring-boot-maven-plugin. - There is even a bug-report on GitHub, that says, that you have to run the application from your favored IDE.
But none of that fixes worked for me.
Some may work, if I would switch my IDE (I am using Netbeans), but I have not tested that, because I am not willing to switch my beloved IDE because of that issue.
The Solution: Move Your Thymeleaf-Templates Back To src/main/webapp
Fortunatly, I found a simple solution, to fix the issue without all the above stuff.
You simply have to move your Thymeleaf-Templates back to where they belong (IMHO): src/main/webapp and turn of the caching.
It is not necessary to run the application in debugging mode and/or from your IDE, nor is it necessary to add the dependency to springloaded or more configuration-switches.
To move the templates and disable caching, just add the following to your application configuration in src/main/application.properties:
spring.thymeleaf.prefix=/thymeleaf/
spring.thymeleaf.cache=false
Of course, you also have to move your Thymeaf-Templates from src/main/resources/templates/ to src/main/webapp/thymeleaf/.
In my opinion, the templates belong there anyway, in order to have them accessible as normal static HTML(5)-files.
If they are locked away in the classpath you cannot access them, which foils the approach of Thymeleaf, that you can view your templates in a browser as thy are.
Funded by the Europian Union
This article was published in the course of a
resarch-project,
that is funded by the European Union and the federal state Northrhine-Wetphalia.