简体   繁体   中英

Automatically reload Thymeleaf templates in Spring Boot app

In my Spring Boot (2.4.2) app, I have some Thymeleaf templates in the src/main/resources/templates directory. I have spring-boot-devtools installed which automatically reloads classes when the code changes.

I would like the Thymeleaf templates to also be automatically reloaded when they are changed. I've tried adding the following to the local application configuration

spring:
  thymeleaf:
    cache: false

But it doesn't seem to work, ie the templates are loaded only on startup, so I need to restart the serve after changing them. How can I enable automatic reloading of the Thymeleaf templates?

Update

In response to some of the comments: I run the app from IntelliJ IDEA.

If you're using IntelliJ, you have to rebuild the project (fn + shift + 9) on mac and (ctrl + shift + f9) on windows I believe. This can be kind of annoying, there might be some built in functionality in IntelliJ, or your IDEA of choice, to just rebuild when certain files are modified.

Something like this should work. Instead of reading your templates from the built files (typically, in your /target folder), you can tell Thymeleaf to read them directly from your /resources folder. That way, you only need to hit F5 to refresh your browser.

# Setup auto-reload of templates for Thymeleaf by disabling the Template Cache.
# This allows you to live-code on HTML without needing to restart the server.
spring:
  thymeleaf: # Thymeleaf
    cache: false
    mode: HTML
    encoding: UTF-8
    # change load path to resource folder instead of /target
    prefix: file:src/main/resources/templates/

  resources: # Static resources
    # change load path to resource folder instead of /target
    static-locations: file:src/main/resources/static/
    cache:
      period: 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM