溫馨提示×

如何在Spring Boot中設(shè)置Thymeleaf的緩存策略

小樊
82
2024-09-12 18:02:47
欄目: 編程語言

在 Spring Boot 中設(shè)置 Thymeleaf 的緩存策略,你需要在 application.properties 或者 application.yml 文件中添加相應(yīng)的配置

  1. 使用 application.properties 文件:
# 關(guān)閉 Thymeleaf 模板緩存
spring.thymeleaf.cache=false

# 設(shè)置模板前綴和后綴
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

# 設(shè)置模板編碼
spring.thymeleaf.encoding=UTF-8
  1. 使用 application.yml 文件:
spring:
  thymeleaf:
    cache: false # 關(guān)閉 Thymeleaf 模板緩存
    prefix: classpath:/templates/ # 設(shè)置模板前綴
    suffix: .html # 設(shè)置模板后綴
    encoding: UTF-8 # 設(shè)置模板編碼

通過將 spring.thymeleaf.cache 屬性設(shè)置為 false,你可以關(guān)閉 Thymeleaf 的模板緩存。這樣,每次請求時都會重新加載模板,有助于開發(fā)過程中實(shí)時查看修改后的頁面效果。在生產(chǎn)環(huán)境中,建議開啟緩存以提高性能。

0