在Spring Boot应用程序中自动加载Thymeleaf模板



在我的Spring Boot(2.4.2)应用程序中,我在src/main/resources/templates目录中有一些百里香模板。我安装了spring-boot-devtools,它会在代码更改时自动重新加载类。

我想胸里叶模板也被自动重新加载时,他们的变化。我尝试将以下内容添加到本地应用程序配置

spring:
thymeleaf:
cache: false

但它似乎不起作用,即模板只在启动时加载,所以我需要在更改它们后重新启动服务。如何启用自动重新加载Thymeleaf模板?

更新回应一些评论:我从IntelliJ IDEA运行应用程序。

这样应该可以工作。您可以告诉Thymeleaf直接从您的/resources文件夹中读取模板,而不是从构建文件中读取模板(通常在/target文件夹中)。这样,你只需要按F5刷新你的浏览器。

# 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

如果你正在使用IntelliJ,你必须在mac上重建项目(fn + shift + 9),我相信在windows上(ctrl + shift + f9)。这可能有点烦人,在IntelliJ中可能有一些内置的功能,或者你选择的IDEA,当某些文件被修改时只重建。

最新更新