1. 程式人生 > >SpringBoot學習-webjars和靜態資源對映規則

SpringBoot學習-webjars和靜態資源對映規則

SpringBoot學習-webjars和靜態資源對映規則

前言

1-以前我們在IDEA中建立一個專案,新增web依賴包,我們現在是一個web應用,應該在man目錄下面有一個webapp資料夾,將所有的頁面都放在這裡,這是我們以前的做法。

2-現在我們建立的這個專案中,沒有這個webapp目錄,但是SpringBoot給我們做了規定。在SpringBoot中對SpringMVC的相關配置都在 WebMvcAutoConfiguration 這個類中做了規定。

3-本文主要內容

  1. /webjars/** ,在 classpath:/META-INF/resources/webjars/ 找資源
  2. "/**" 訪問當前專案的任何資源,都去(靜態資源的資料夾)找對映
  3. 歡迎頁; 靜態資原始檔夾下的所有index.html頁面;被"/**"對映;
  4. favicon.ico :所有的 **/favicon.ico 都是在靜態資原始檔下找

 

 正文

 

1-/webjars/** ,在 classpath:/META-INF/resources/webjars/ 找資源

1-1-原始碼分析

public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {

private final ResourceProperties resourceProperties; //1-配置靜態訪問資源 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(); 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)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); //靜態資原始檔夾對映 if (!registry.hasMappingForPattern(staticPathPattern)) { this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl)); } } } }

1-2-webjars:以jar包的方式引入靜態資源,可以去http://www.webjars.org/ 這個網站選擇自己的靜態資源

1-2-1-去webjars官網獲取自己需要的maven依賴

1-2-2-將maven依賴匯入行pom.xml檔案

1-2-3-啟動專案,測試是否可以獲取jquery資源(示例路徑:http://localhost:8080/webjars/jquery/3.3.1/jquery.js)

 

 

2-自定義靜態檔案的對映路徑(意思為:我們把靜態檔案放在這些路徑下面,就會被載入到)

2-1-預設對映路徑:"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/";優先順序順序為:META-INF/resources > resources > static > public

2-2-預設對映路徑,原始碼出處

//ResourceProperties類
@ConfigurationProperties(
    prefix = "spring.resources",//如果要自己配置路徑,在配資字首為"spring.resources"
    ignoreUnknownFields = false
)
public class ResourceProperties {
    //聲明瞭預設classpath資源路徑為:{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

}

 2-3-應用示例(http://localhost:8080/demo.jpg指的是依次去"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"去找demo.jpg圖片)

2-4-修改預設靜態資源路徑,方法一:配置spring.resources.static-locations引數

#在application.properities中進行配置,多個路徑可以用逗號隔開
spring.resources.static-locations=classpath:/myresources/

 

2-5-修改預設靜態資源路徑,方法二:重寫WebMvcConfigurerAdapter 中的addResourceHandlers方法,自定義對映路徑

2-5-1-重寫WebMvcConfigurerAdapter 中的addResourceHandlers方法,自定義對映路徑

@Configuration
public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
    /**
     * 配置靜態訪問資源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/mypath/**").addResourceLocations("classpath:/myresources/");
        super.addResourceHandlers(registry);
    }
}

 

2-5-2-應用示例

 

 

 

3-歡迎頁面(當瀏覽器輸入專案路徑,無法匹配正確路徑,會去歡迎頁面)

3-1-原始碼分析

public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {

    private final ResourceProperties resourceProperties;

    //1-指定this.getWelcomePage(),見2
    @Bean
    public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
        return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
    }
    //2-去getIndexHtml()獲取歡迎頁面路徑,見3
    private Optional<Resource> getWelcomePage() {
        String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
        return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
    }
    //3-預設歡迎頁面在location(對映路徑) + "index.html"
    private Resource getIndexHtml(String location) {
        return this.resourceLoader.getResource(location + "index.html");
    }

}

3-2-應用示例

 

4-favicon.ico 

4-1-所有的 **/favicon.ico 都是在靜態資原始檔下找

4-1-應用示例

 

參看資料:

1-https://blog.csdn.net/baidu_36216018/article/details/79699084

2-https://www.cnblogs.com/java-synchronized/p/7091723.html

3-https://blog.csdn.net/javareact/article/details/77981769