繁体   English   中英

在 Spring 启动应用程序中自动重新加载 Thymeleaf 模板

[英]Automatically reload Thymeleaf templates in Spring Boot app

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

我希望 Thymeleaf 模板在更改时也能自动重新加载。 我尝试将以下内容添加到本地应用程序配置中

spring:
  thymeleaf:
    cache: false

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

更新

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

如果您使用的是 IntelliJ,您必须在 mac 上重建项目 (fn + shift + 9) 并在 windows 上重建项目 (ctrl + shift + f9) 我相信。 这可能有点烦人,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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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