Springboot學習01-webjars和靜態資源映射規則
前言
1-以前我們在IDEA中創建一個項目,添加web依賴包,我們現在是一個web應用,應該在man目錄下面有一個webapp文件夾,將所有的頁面都放在這里,這是我們以前的做法。
2-現在我們創建的這個項目中,沒有這個webapp目錄,但是SpringBoot給我們做了規定。在SpringBoot中對SpringMVC的相關配置都在 WebMvcAutoConfiguration 這個類中做了規定。
3-本文主要內容
- /webjars/** ,在 classpath:/META-INF/resources/webjars/ 找資源
- "/**" 訪問當前項目的任何資源,都去(靜態資源的文件夾)找映射
- 歡迎頁; 靜態資源文件夾下的所有index.html頁面;被"/**"映射;
- 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