最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用代碼解析Springboot靜態(tài)資源訪問

這篇文章主要用代碼解析Springboot靜態(tài)資源訪問,內(nèi)容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

創(chuàng)新互聯(lián)公司是一家專注網(wǎng)站建設、網(wǎng)絡營銷策劃、小程序設計、電子商務建設、網(wǎng)絡推廣、移動互聯(lián)開發(fā)、研究、服務為一體的技術型公司。公司成立十年以來,已經(jīng)為上1000家公路鉆孔機各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務?,F(xiàn)在,服務的上1000家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。

springboot靜態(tài)資源加載默認是從/static(或/public或/resources或/META-INF/resources) 目錄下加載靜態(tài)資源。

加載的優(yōu)選級別:/META-INF/resources》/resources》/public》/static

靜態(tài)資源的加載源碼分析(WebMvcAutoConfiguration類)

首先從WebMvcAutoConfiguration.class自動配置類部分代碼來看:

//添加靜態(tài)資源規(guī)則
public void addResourceHandlers(ResourceHandlerRegistry registry) {
      if (!this.resourceProperties.isAddMappings()) {
        logger.debug("Default resource handling disabled");
      } else {
        Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
        CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
        //webjars依賴映射規(guī)則
        if (!registry.hasMappingForPattern("/webjars/**")) {
          this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }
				//本地配置的映射規(guī)則
        //this.resourceProperties.getStaticLocations() 從ResourceProperties中加載靜態(tài)路徑
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
          this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations            this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

      }
    }

ResourceProperties類部分源碼

@ConfigurationProperties(
  prefix = "spring.resources",
  ignoreUnknownFields = false
)
public class ResourceProperties {
  //springboot默認的加載路徑
  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
  private String[] staticLocations;
  private boolean addMappings;
  private final ResourceProperties.Chain chain;
  private final ResourceProperties.Cache cache;

映射規(guī)則總結

在springboot靜態(tài)資源加載規(guī)則中,除了”標準“靜態(tài)資源位置之外,還有一個較為特殊的WebJars

“標準”靜態(tài)資源映射規(guī)則

​所有的“/**”的請求,如果沒有對應的處理,那么就去默認映射的靜態(tài)資源目錄下去找,如下所示:

  1. "classpath:/META-INF/resources/"
  2. "classpath:/resources/"
  3. "classpath:/static/",
  4. "classpath:/public/"
  5. “/**”

所有的webjars的請求都會去 ”classpath:/META-INF/resources/webjars/**“去資源

(如果 以jar包的方式來引入jquery包)

在pom.xml中引入依賴

 
      org.webjars
      jquery
      3.3.1-2
    

從引入的包目錄來看

用代碼解析Springboot靜態(tài)資源訪問

springboot默認歡迎頁面

自動去加載默認目錄下的index.html;如static/index.html

自定義配置靜態(tài)資源目錄

在application.properties文件中去配置

//配置test為靜態(tài)資源目錄
spring.resources.static-locations=classpath:/test/

遇到的坑

在配置了靜態(tài)資源目錄的時候,跳轉到的頁面路徑不能寫絕對路徑,

比如:spring.resources.static-locations=classpath:/test/ 我配置test為靜態(tài)資源的加載位置,在訪問的時候不需要寫test

用代碼解析Springboot靜態(tài)資源訪問

請求:http://127.0.0.1:8085/test/1.png

用代碼解析Springboot靜態(tài)資源訪問

請求:http://127.0.0.1:8085/1.png

用代碼解析Springboot靜態(tài)資源訪問

以上就是關于用代碼解析Springboot靜態(tài)資源訪問的內(nèi)容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。


網(wǎng)站名稱:用代碼解析Springboot靜態(tài)資源訪問
本文地址:http://fisionsoft.com.cn/article/gsdijo.html