SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀
Spring Boot對靜態(tài)資源的映射規(guī)則
1.所有/webjars/**,都去classpath:/META-INF/resources/webjars/找資源
webjars:以jar包方式引入靜態(tài)資源。
<!‐‐引入jquery‐webjar,在訪問的時(shí)候只需要寫webjars下面資源的名稱即可‐‐> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.1</version> </dependency>
- 訪問路徑://127.0.0.1:8089/webjars/jquery/3.4.1/jquery.js
2. "/**" 訪問當(dāng)前項(xiàng)目的任何資源,都去(靜態(tài)資源的文件夾)找映射
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當(dāng)前項(xiàng)目的根路徑
localhost:8080/abc === 去靜態(tài)資源文件夾里面找abc
3. 歡迎頁:靜態(tài)資源文件夾下的所有的index.html頁面;被 /** 映射
localhost:8089/:找首頁(index.html)
4. 所有的 **/favicon.ico都是在靜態(tài)資源文件下找的。
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); //靜態(tài)資源文件夾映射 if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)); } }
配置歡迎頁映射
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(getInterceptors()); return welcomePageHandlerMapping; }
@Configuration @ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true) public static class FaviconConfiguration implements ResourceLoaderAware { private final ResourceProperties resourceProperties; private ResourceLoader resourceLoader; public FaviconConfiguration(ResourceProperties resourceProperties) { this.resourceProperties = resourceProperties; } @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } // 映射**/favicon.ico @Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler())); return mapping; } @Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler(); requestHandler.setLocations(resolveFaviconLocations()); return requestHandler; } private List<Resource> resolveFaviconLocations() { String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations()); List<Resource> locations = new ArrayList<>(staticLocations.length + 1); Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add); locations.add(new ClassPathResource("/")); return Collections.unmodifiableList(locations); } } }
到此這篇關(guān)于SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)資源的映射規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 內(nèi)省(Introspector)深入理解
這篇文章主要介紹了Java 內(nèi)省(Introspector)深入理解的相關(guān)資料,需要的朋友可以參考下2017-03-03Spring Task定時(shí)任務(wù)的配置和使用詳解
本篇文章主要介紹了Spring Task定時(shí)任務(wù)的配置和使用詳解,實(shí)例分析了Spring Task定時(shí)任務(wù)的配置和使用的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目
這篇文章主要介紹了基于SpringBoot與Mybatis實(shí)現(xiàn)SpringMVC Web項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2017-04-04Hibernate雙向一對一映射關(guān)系配置代碼實(shí)例
這篇文章主要介紹了Hibernate雙向一對一映射關(guān)系配置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10如何使用Spring Cloud Feign日志查看請求響應(yīng)
這篇文章主要介紹了如何使用Spring Cloud Feign日志查看請求響應(yīng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02Java ArrayDeque實(shí)現(xiàn)Stack的功能
這篇文章主要介紹了Java ArrayDeque實(shí)現(xiàn)Stack功能的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03