1. 概述
- spring 配合 thymeleaf 關閉頁面緩存
2. 背景
-
最近復習 spring
-
找了本書叫 spring in action 5th
- 本人水平有限
- 書還寫得那么難
-
調試中遇到了問題
- 問題1: 我每次改代碼, 都要重啟
- 問題2: 我每次改頁面, 都要重啟
-
因為 問題2 對我來說, 優先級最高, 我首先解決它
3. 環境
-
os
- win10
-
jdk
- 1.8
-
ide
- ida 2018.1
-
spring
- spring boot
- 2.1.7 release
- 組件
- thymeleaf
- starter-web
- devtool
- starter-test
- spring boot
-
browser
- firefox
- 70.0
- firefox
-
ref
- spring in action 5th
4. 問題
-
期望
- 在 ide 修改頁面
- 在瀏覽器直接刷新, 就能看到頁面調整
-
需要保證
- 瀏覽器的正確配置
- 瀏覽器
- 確保每次請求, 都是新鮮的頁面
- 瀏覽器
- spring 和 thymeleaf 的正確配置
- spring
- 確保 thymeleaf 的修改能及時
- spring
- 瀏覽器的正確配置
5. 問題解決
-
瀏覽器
-
思路1: 每次訪問后清空緩存
- 結果
- 否決
- 太麻煩了
- 否決
- 結果
-
思路2: 通過瀏覽器配置, 確保每次請求, 都是新頁面
-
結果
- 這個方案不錯
- 而且也有配置項可以做到
- 因為很順利, 就不細說了, 照着配置一步步來就行
-
ref
-
-
-
thymeleaf
- 思路: 找到配置項, 配置一下就好了
-
結果
- 媽的找死我了
- 百度了好多, 都沒用
-
千篇一律, 只有一條參數
spring.thymeleaf.cache=false -
結果配置了根本不生效
-
這感覺太難受了, 真的想罵人
-
-
最后結果
- ref
-
Thymeleaf templates cache even when spring.template.cache: false
-
一個叫 oak1980 的丹麥老哥, 給了一個配置如下
# Templates reloading during development spring.thymeleaf.prefix=file:src/main/resources/templates/ spring.thymeleaf.cache=false # Static resources reloading during development spring.resources.static-locations=file:src/main/resources/static/ spring.resources.cache-period=0
-
-
按照這個老哥的做法, 配置終於生效
-
- ref
-
- 思路: 找到配置項, 配置一下就好了
ps
-
媽的為啥我百度的頭幾個, 沒有一個把事說明白
- 大部分只給了 cache = false 的那條配置
- 其他提到 prefix 的, 還都把 prefix 注釋了
- 配置的 ide, 組件, 版本也沒有
- 找了好半天, 問題是這種都是調試常識之類的東西
-
spring 如何確認運行階段生效了哪些配置項
- 這個找了半天, 也沒找到
- 我總覺得可以...
