簡體   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