目錄
一、什麼是靜態資源?
一般客戶端發送請求到web服務器,web服務器從內存在取到相應的文件,返回給客戶端,客戶端解析並渲染顯示出來。
例如:css,js等文件。
二、使用靜態資源
第一種:使用webjars
SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 這個配置類裏面,默認爲我們提供了靜態資源處理。
我們進入WebMvcAutoConfigurationAdapter源碼裏面進行查看,發現有這麼一個方法:
addResourceHandlers 添加資源處理;
@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();
// webjars 配置
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// 靜態資源配置
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
分析:
所有的 /webjars/** , 都需要去 classpath:/META-INF/resources/webjars/ 找對應的資源;
什麼是webjars 呢?
Webjars本質就是以jar包的方式引入我們的靜態資源 。
使用
當我們需要使用某一個靜態資源時,只需要去以下的網站查找相對應的版本,然後將其導入即可。
網站:WebJars - Web Libraries in Jars
例如:
我們現在需要使用jQuery這個靜態資源,那麼我們前往官網查找。
我們將其對應的Maven依賴複製到pom.xml中
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
導入完畢後,我們去查看webjars目錄結構。
正如上面源碼分析知道,所有的 /webjars/** , 都需要去 classpath:/META-INF/resources/webjars/ 找對應的資源;
那麼我們現在去訪問Jquery.js文件。
注意:只要是靜態資源,SpringBoot就會去對應的路徑(classpath:/META-INF/resources/webjars/ )尋找資源
瀏覽器訪問:http://localhost:8080/webjars/jquery/3.4.1/jquery.js
訪問成功!
第二種:將靜態資源文件放至靜態資源文件夾
源碼:
依舊是在WebMvcAutoConfiguration.class類中,我們進行查看:
那我們點進去看一下這是給我們配置到哪裏去了呢?
圖片續:
經過一路跟蹤,終於找到,springboot爲我們自動配置的靜態資源位置是如下幾個:classpath:/MATA-INF/resources/、classpath:/resources/、classpath:/public/、classpath:/static/這幾個位置,無論我們把靜態資源放到這幾個的哪一個都可以訪問到。
也就是說以下四個目錄存放的靜態資源可以被我們識別:
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
我們可以在resources根目錄下新建對應的文件夾,都可以存放我們的靜態文件;
例如:
接着我們去訪問該靜態資源:
http://localhost:8080/hello.js
訪問成功!
第三種:自定義靜態資源路徑
我們可以通過application.properties配置文件來指定一下,哪些文件夾是需要我們放靜態資源文件的。
(1)編寫配置文件
application.properties:
spring.web.resources.static-locations=classpath:/yixin/,classpath:/king/
我們定義了兩個存放靜態資源的目錄:yixin和king
(2)創建目錄
(3)測試
訪問成功!!
注意:一旦自己定義了靜態文件夾的路徑,原來的自動配置就都會失效了!
三、首頁處理
我們去查看一下WebMvcAutoConfigurationAdapter的源碼,發現有以下方法:
也是就是說當我們啓動SpringBoot的時候,去訪問http://localhost:8080/,那麼就會去找靜態資源文件夾下的 index.html。
我們進行測試一下
(1)編寫index.html並放在任意的靜態資源文件夾中
(2)index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好呀,我是一心同學。
</body>
</html>
(3)訪問http://localhost:8080/
訪問成功!
結語
以上就是一心同學對SpringBoot中Web的靜態資源的知識整理,靜態資源的訪問會在我們進行WEB開發中經常使用的,務必掌握其訪問的方法。