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.
Best solution ever!
December 2019, still works.
excellent!
after doing every other way to fix it, your way worked at once… sincerely i dont understand why there are so much invalid ways to do it, and yours that’s the simplest one no one mentions it…
Thanks a lot my friend!
Thank you! Been banging my head on this for hours.
Thank you, what I did to solve this issue is to simply set this two lines in the beginning of the main function. (using netbeans too )
System.setProperty(“spring.template.cache”, “false”);
System.setProperty(“spring.thymeleaf.cache”, “false”);
I understand your point, if you deliver the app as war file, which will deployed inside a tomcat webapp dir.
But what if you want to deliver just a single, runnable jar?
And
spring.thymeleaf.cache = falsedid quite a good job while developing. (using spring boot 1.3.2)kind regards
I am not sure, what you are objecting against.
Does it work with spring boot 1.3.2 without moving the templates? Or do you say, that you do not need your templates to be accessible, when you deliver your app?
Thank you very much!
Your advice helped me a lot with Netbeans 8.0.2. :-)