【SpringBoot學習之路】10.Web開發之靜態資源的對映規則
阿新 • • 發佈:2018-11-24
轉載宣告:商業轉載請聯絡作者獲得授權,非商業轉載請註明出處.原文來自 © 呆萌鍾【SpringBoot學習之路】10.Web開發之靜態資源的對映規則
簡介
使用SpringBoot
- 建立SpringBoot應用,選中我們需要的模組;
- SpringBoot已經預設將這些場景配置好了,只需要在配置檔案中指定少量配置就可以執行起來
- 自己編寫業務程式碼;
自動配置原理?
這個場景SpringBoot幫我們配置了什麼?能不能修改?能修改哪些配置?能不能擴充套件?
xxxxAutoConfiguration:幫我們給容器中自動配置元件; xxxxProperties:配置類來封裝配置檔案的內容;
SpringBoot對靜態資源的對映規則
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
public class ResourceProperties {
//可以設定和靜態資源有關的引數,快取時間等
public class WebFluxAutoConfiguration { public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); } else { if (!registry.hasMappingForPattern("/webjars/**")) { ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}); this.configureResourceCaching(registration); this.customizeResourceHandlerRegistration(registration); } String staticPathPattern = this.webFluxProperties.getStaticPathPattern(); //靜態資原始檔夾對映 if (!registry.hasMappingForPattern(staticPathPattern)) { ResourceHandlerRegistration registration = registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()); this.configureResourceCaching(registration); this.customizeResourceHandlerRegistration(registration); } } } //配置喜歡的圖示 public class WebMvcAutoConfiguration { public static class FaviconConfiguration implements ResourceLoaderAware { private final ResourceProperties resourceProperties; private ResourceLoader resourceLoader; public FaviconConfiguration(ResourceProperties resourceProperties) { this.resourceProperties = resourceProperties; } public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } @Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(-2147483647); //所有 **/favicon.ico mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler())); return mapping; } @Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler(); requestHandler.setLocations(this.resolveFaviconLocations()); return requestHandler; }
所有 /webjars/ ,都去 classpath:/META-INF/resources/webjars/ 找資源;
webjars:以jar包的方式引入靜態資源;
http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js可以直接訪問到js檔案內容
<!‐‐引入jquery‐webjar。在訪問的時候只需要寫webjars下面資源的名稱即可‐‐> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1-1</version> </dependency>
"/" 訪問當前專案的任何資源,都去(靜態資源的資料夾)找對映
"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":當前專案的根路徑
localhost:8080/abc === 去靜態資原始檔夾裡面找abc
歡迎頁; 靜態資原始檔夾下的所有index.html頁面;被"/"對映;
localhost:8080/ 找index頁面
所有的 **/favicon.ico 都是在靜態資原始檔下找;