SpringBoot 配置靜態文件緩存

yml配置

spring:
  resources:
    chain:
      strategy:
        content:
          enabled: true
          paths: /**
      cache: true
      compressed: false
      enabled: true
    cache:
      cachecontrol:
        cache-public: true

增加代碼配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.resource.ResourceUrlProvider;

@ControllerAdvice
public class ControllerConfig {
    @Autowired
    ResourceUrlProvider resourceUrlProvider;

    @ModelAttribute("urls")
    public ResourceUrlProvider urls() {
        return this.resourceUrlProvider;
    }
}

修改靜態文件引入fangshi

<link rel="stylesheet" href="${urls.getForLookupPath('/css/index/index.css')}">

不足之處

只能對通過controller進入的頁面生效,攔截器直接跳轉的頁面無效(跳轉到頁面的URL然後通過controller進入頁面是有效的,直接進入頁面是無效的)

當前版本

  • Spring Boot 5.1.5.RELEASE
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章