Spring - MVC - thymeleaf 緩存關閉


1. 概述

  1. spring 配合 thymeleaf 關閉頁面緩存

2. 背景

  1. 最近復習 spring

  2. 找了本書叫 spring in action 5th

    1. 本人水平有限
    2. 書還寫得那么難
  3. 調試中遇到了問題

    1. 問題1: 我每次改代碼, 都要重啟
    2. 問題2: 我每次改頁面, 都要重啟
  4. 因為 問題2 對我來說, 優先級最高, 我首先解決它

3. 環境

  1. os

    1. win10
  2. jdk

    1. 1.8
  3. ide

    1. ida 2018.1
  4. spring

    1. spring boot
      1. 2.1.7 release
    2. 組件
      1. thymeleaf
      2. starter-web
      3. devtool
      4. starter-test
  5. browser

    1. firefox
      1. 70.0
  6. ref

    1. spring in action 5th

4. 問題

  1. 期望

    1. 在 ide 修改頁面
    2. 在瀏覽器直接刷新, 就能看到頁面調整
  2. 需要保證

    1. 瀏覽器的正確配置
      1. 瀏覽器
        1. 確保每次請求, 都是新鮮的頁面
    2. spring 和 thymeleaf 的正確配置
      1. spring
        1. 確保 thymeleaf 的修改能及時

5. 問題解決

  1. 瀏覽器

    1. 思路1: 每次訪問后清空緩存

      1. 結果
        1. 否決
          1. 太麻煩了
    2. 思路2: 通過瀏覽器配置, 確保每次請求, 都是新頁面

      1. 結果

        1. 這個方案不錯
        2. 而且也有配置項可以做到
        3. 因為很順利, 就不細說了, 照着配置一步步來就行
      2. ref

        1. 設置火狐瀏覽器不緩存js與html
  2. thymeleaf

    1. 思路: 找到配置項, 配置一下就好了
      1. 結果

        1. 媽的找死我了
        2. 百度了好多, 都沒用
          1. 千篇一律, 只有一條參數

            spring.thymeleaf.cache=false
            
          2. 結果配置了根本不生效

          3. 這感覺太難受了, 真的想罵人

      2. 最后結果

        1. ref
          1. Thymeleaf templates cache even when spring.template.cache: false

            1. 一個叫 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
              
          2. 按照這個老哥的做法, 配置終於生效

ps

  1. 媽的為啥我百度的頭幾個, 沒有一個把事說明白

    1. 大部分只給了 cache = false 的那條配置
    2. 其他提到 prefix 的, 還都把 prefix 注釋了
    3. 配置的 ide, 組件, 版本也沒有
    4. 找了好半天, 問題是這種都是調試常識之類的東西
  2. spring 如何確認運行階段生效了哪些配置項

    1. 這個找了半天, 也沒找到
    2. 我總覺得可以...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM