温馨提示×

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

小樊
82
2024-09-12 18:02:47
栏目: 编程语言

在 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 的模板缓存。这样,每次请求时都会重新加载模板,有助于开发过程中实时查看修改后的页面效果。在生产环境中,建议开启缓存以提高性能。

0