如何在Spring Boot中设置Thymeleaf的缓存策略

233
2024/9/12 18:31:21
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在 Spring Boot 中设置 Thymeleaf 的缓存策略,你需要在 application.properties 或者 application.yml 文件中添加相应的配置

  1. 使用 application.properties 文件:
# 关闭 Thymeleaf 模板缓存
spring.thymeleaf.cache=false

# 设置模板前缀和后缀
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

# 设置模板编码
spring.thymeleaf.encoding=UTF-8
  1. 使用 application.yml 文件:
spring:
  thymeleaf:
    cache: false # 关闭 Thymeleaf 模板缓存
    prefix: classpath:/templates/ # 设置模板前缀
    suffix: .html # 设置模板后缀
    encoding: UTF-8 # 设置模板编码

通过将 spring.thymeleaf.cache 属性设置为 false,你可以关闭 Thymeleaf 的模板缓存。这样,每次请求时都会重新加载模板,有助于开发过程中实时查看修改后的页面效果。在生产环境中,建议开启缓存以提高性能。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Profiles在Spring Boot中的优先级排序