Springboot學習02-webjars和靜態資源映射規則

Springboot學習01-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依賴html

1-2-2-將maven依賴導入行pom.xml文件java

1-2-3-啓動項目,測試是否能夠獲取jquery資源(示例路徑:http://localhost:8080/webjars/jquery/3.3.1/jquery.js)react

 

 

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方法,自定義映射路徑jquery

@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-應用示例web

 

 

 

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-應用示例

 

參看資料:spring

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

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

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

相關文章
相關標籤/搜索