Fix Hot Reload of Thymeleaf-Templates In spring-boot:run

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=false in your application-configuration in src/main/resources/application.properties.
  • Some say, that you have to disable caching by setting spring.template.cache=false and spring.thymeleaf.cache=false and/or run the application in debugging mode.
  • Others say, that you have to add a dependency to org.springframework:springloaded to the configuration of the spring-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.

Europäische Union: Investitionen in unsere Zukunft - Europäischer Fonds für regionale Entwicklung EFRE.NRW 2014-2020: Invesitionen in Wachstum und Beschäftigung

Comments / Questions

  1. Hodglem says:

    Thank you! Been banging my head on this for hours.

  2. Kai Tønder says:

    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”);

  3. Benny says:

    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 = false did quite a good job while developing. (using spring boot 1.3.2)

    kind regards

    1. Kai Moritz says:

      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?

  4. Jan says:

    Thank you very much!
    Your advice helped me a lot with Netbeans 8.0.2. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>