springBoot快速訪問工程目錄下的靜態(tài)資源
1、牛刀小試
1.1 圖片靜態(tài)資源的訪問
先看官方怎么說,點擊鏈接,打開SpringBoot官方文檔
文檔中明確指出:/static
(or/public
or/resources
or/META-INF/resources
) ,這幾個目錄是SpringBoot放置靜態(tài)資源的目錄,只要把靜態(tài)資源放到這幾個目錄下,就能直接訪問到。
新建 Spingboot web項目試下,新項目只有 /static 目錄 ,手動創(chuàng)建其他幾個靜態(tài)資源文件夾,每個目錄添加1張圖片
啟動項目,分別訪問這四張圖片:
發(fā)現(xiàn)圖片均可訪問,
文檔說的對,果然沒騙人,
由此我們認(rèn)定 SpringBoot 訪問靜態(tài)資源:當(dāng)前項目根路徑 + / + 靜態(tài)資源名
1.2 為靜態(tài)資源添加訪問前綴
By default, resources are mapped on /**, but you can tune that with the spring.mvc.static-path-pattern property. For instance, relocating all resources to /resources/** can be achieved as follows: PropertiesYaml spring.mvc.static-path-pattern=/resources/**
文檔又解釋了一下,說,默認(rèn)情況下SpringBoot是幫你映射的路徑是/** ,
但是,如果你想加一個前綴也可以,比如 /res/
技術(shù)圈有句話:先有業(yè)務(wù)才有技術(shù),SpringBoot官方考慮到某些網(wǎng)站添加了登錄驗證,一般需要登錄后才能訪問項目中的資源,為了登錄頁樣式也能正常顯示,方便放行靜態(tài)資源,直接給所有靜態(tài)資源添加一個前綴,既可統(tǒng)一攔截,又可統(tǒng)一放開
操作:在配置文件application.properties中添加
spring.mvc.static-path-pattern=/res/**
添加完再去訪問原來的dog圖片鏈接:http://localhost:8080/dog.jpeg
但是訪問:http://localhost:8080/res/dog.jpeg發(fā)現(xiàn)這才可以
嘿嘿😋
1.3 WelCome Page 的奇妙跳轉(zhuǎn)
7.1.6. Welcome Page
Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.
文檔說把一個名稱叫 index.html 的文件放到任意的靜態(tài)目錄下,訪問http://localhost:8080即可到達(dá),意思就是給你一個首頁跳轉(zhuǎn)的快捷方式(注意:需把1.2 的配置路徑去掉,否則會導(dǎo)致welcome page功能失效,后面源碼分析會說到)
新建html,放到 /static 下,訪問:
2、那么,SpringBoot是如何做到的呢?
接下來看源碼探究 SpringBoot 靜態(tài)資源配置原理 》》》》 gogogo
源碼位置在:spring-boot-autoconfigure-2.5.1.jar 這個jar里面,具體的目錄如下:
/spring-boot-autoconfigure-2.5.1.jar!/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class
WebMvcAutoConfiguration 類里面找到addResourceHandlers 方法,顧名思義 添加資源處理器
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 相當(dāng)于一個開關(guān)去控制靜態(tài)資源處理器的加載,默認(rèn)為true,設(shè)置為false就會禁止所有規(guī)則 if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } //第一個就配置webjars的訪問規(guī)則,規(guī)定在類路徑的/META-INF/resources/webjars/路徑下,感興趣的同學(xué)可以點進(jìn)方法去,里面還配置了webjars的瀏覽器端緩存時間,是在application。propertities中的一個配置項 spring.web.resources.cache.period addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/"); //這里配置了靜態(tài)資源的四個訪問路徑 addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> { registration.addResourceLocations(this.resourceProperties.getStaticLocations()); if (this.servletContext != null) { ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION); registration.addResourceLocations(resource); } }); }
第一個if判斷this.resourceProperties.isAddMappings() 去配置文件獲取
spring.resources 這個屬性,默認(rèn)是 true , 如果設(shè)置為false 那么就等于禁用掉所有的靜態(tài)資源映射功能,不行就試一下
#springapplication.propertities中配置 spring.web.resources.add-mappings=false
重啟項目,發(fā)現(xiàn)首頁無法訪問了...
改回 true ,首頁就又可以訪問了
不要停留,繼續(xù)看第二個addResourceHandler 方法,打斷點看看這個方法添加了什么規(guī)則
沒錯,第二個addResourceHandler 方法就表明 / **下的所有請求,都在這四個默認(rèn)的位置去找靜態(tài)資源映射 ,這四個目錄在官方文檔中提到過。
另外,訪問路徑前綴是在 this.mvcProperties.getStaticPathPattern() 獲取的,配置上:
spring.mvc.static-path-pattern=/res/**
打斷點如下:
注意📢: 所有的請求先去controller控制器找映射,找不到,再來靜態(tài)資源映射器。
到這里解決了靜態(tài)資源目錄的問題。
馬不停蹄,探究 Welcome Page 的事情 》》》》》
還是在 WebMvcAutoConfiguration 這個類:搜索 “WelcomePage” :
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations()); return welcomePageHandlerMapping; }
把WelcomePageHandlerMapping 的有參構(gòu)造也拿來
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders, ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) { if (welcomePage != null && "/**".equals(staticPathPattern)) { logger.info("Adding welcome page: " + welcomePage); setRootViewName("forward:index.html"); } else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) { logger.info("Adding welcome page template: index"); setRootViewName("index"); } }
根據(jù)有參構(gòu)造可以看出來,只有 歡迎頁這個資源存在,并且 靜態(tài)資源訪問路徑是 /** ,才能重定向到indes.html ,否則就會去找 Controller 處理。
這就解釋了,上面為什么配置了靜態(tài)資源訪問路徑 為/res/** 后導(dǎo)致首頁無法訪問到 的問題
好了,前面牛刀小試的坑已經(jīng)填完了,關(guān)于SpringBoot 靜態(tài)資源配置原理 這篇總結(jié)就到這了
以上就是SprignBoot訪問工程目錄下的靜態(tài)資源的詳細(xì)內(nèi)容,更多關(guān)于SprignBoot訪問靜態(tài)資源的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot 返回json格式數(shù)據(jù)時間格式配置方式
這篇文章主要介紹了springboot 返回json格式數(shù)據(jù)時間格式配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11大數(shù)據(jù)Kafka:消息隊列和Kafka基本介紹
本文對消息隊列的應(yīng)用場景,優(yōu)缺點,消息隊列的兩種方式,常見的消息隊列產(chǎn)品以及Kafka的特點和應(yīng)用場景做了詳細(xì)的講解,需要的朋友可以參考下,希望可以對大家有所幫助2021-08-08Java實現(xiàn)EasyCaptcha圖形驗證碼的具體使用
Java圖形驗證碼,支持gif、中文、算術(shù)等類型,可用于Java Web、JavaSE等項目,下面就跟隨小編一起來了解一下2021-08-08