前兩章我們分享了spring boot對restful 的支持,不過restful的接口通常僅僅返回數據。而做web開發的時候,我們往往會有很多靜態資源,如html、圖片、css等。那如何向前端返回靜態資源呢?以前做過web開發的同學應該知道,我們以前創建的web工程下面會有一個webapp的目錄,我們只要把靜態資源放在該目錄下就可以直接訪問。但是,基于spring boot的工程并沒有這個目錄,那我們應該怎么處理?
一、最笨的方式
我們首先來分享一種最笨的辦法,就是將靜態資源通過流直接返回給前端,我們在maven工程的resources的根目錄下建立一個html的目錄,然后我們把html文件放在該目錄下,并且規定任何訪問路徑以/static/開頭的即訪問該目錄下的靜態資源,其實現如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
@controller public class staticresourcecontroller { @requestmapping ( "/static/**" ) public void gethtml(httpservletrequest request, httpservletresponse response) { string uri = request.getrequesturi(); string[] arr = uri.split( "static/" ); string resourcename = "index.html" ; if (arr.length > 1 ) { resourcename = arr[ 1 ]; } string url = staticresourcecontroller. class .getresource( "/" ).getpath() + "html/" + resourcename; try { filereader reader = new filereader( new file(url)); bufferedreader br = new bufferedreader(reader); stringbuilder sb = new stringbuilder(); string line = br.readline(); while (line != null ) { sb.append(line); line = br.readline(); } response.getoutputstream().write(sb.tostring().getbytes()); response.flushbuffer(); } catch (ioexception e) { e.printstacktrace(); } } } |
其實現過程很簡單,就是先從路徑中分離出來資源uri,然后從static目錄下讀取文件,并輸出到前端。因為只做簡單演示,所以這里只處理了文本類型的文件,圖片文件可以做類似的處理。當然,我們在實際中肯定不會這么做,spring boot也肯定有更好的解決辦法。不過這個辦法雖然有點笨,但確是最本質的東西,無論框架如何方便的幫我們處理了這類問題,但是拋開框架,我們依然要能夠熟練的寫出一個web項目,只有知道其實現原理,你才會在遇到問題時能得心應手。現在我們再來看看spring boot對靜態資源的支持。
二、spring boot默認靜態資源訪問方式
spring boot默認對/**的訪問可以直接訪問四個目錄下的文件:
classpath:/public/
classpath:/resources/
classpath:/static/
classpath:/meta-info/resouces/
我們現在就在資源文件resources目錄下建立如下四個目錄:
注意藍色條下的資源文件夾resources與類路徑下的文件夾classpath:/resources是不同的,藍色條下的resources代表的是該目錄下的文件為資源文件,在打包的時候會將該目錄下的文件全部打包的類路徑下,這個名稱是可以改的,在pom.xml指定資源目錄即可:
1
2
3
4
5
|
<resources> <resource> <directory>src/main/resources</directory> </resource> </resources> |
而類路徑下的resources是spring boot默認的靜態資源文件夾之一,和public、static以及meat-info/resources的功能相同。現在我們重啟spring boot就可以通過
http://localhost:8080/1.html
http://localhost:8080/2.html
http://localhost:8080/3.html
http://localhost:8080/4.html
四個url訪問到四個目錄下的靜態資源了。
三、自定義靜態資源目錄
通過第二節內容我們已經知道了spring boot默認可以訪問的靜態資源的目錄,但是大家肯定會想,這個目錄是固定的嗎?我們可不可以自己定義靜態資源目錄?答案是肯定的,我們現在就來自定義一個靜態資源目錄,我們定義一個images的目錄來存放圖片,所有/image/**的路徑都會訪問images目錄下的資源:
1
2
3
4
5
6
7
8
9
|
@configuration public class imagemvcconfig extends webmvcconfigureradapter { @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler( "/image/**" ) .addresourcelocations( "classpath:/images/" ); } } |
這段代碼應該比較簡單,@configuration標識一個配置類,這個在前面的文章中提到過多次。webmvcconfigureradapter是spring提供的一個配置mvc的適配器,里面有很多配置的方法,addresourcehandlers就是專門處理靜態資源的方法,其他方法后續我們還會講到。現在我們在驗證上面的配置是否有效。我在images目錄下放了一張spring.jpg的圖片,現在我們通過http://localhost:8080/image/spring.jpg來訪問圖片:
其實除了上面的辦法還有一種更簡單的辦法,就是直接在application.yml中配置即可:
1
2
3
4
5
|
spring: mvc: static -path-pattern: /image/** resources: static -locations: classpath:/images/ |
static-path-pattern:訪問模式,默認為/**,多個可以逗號分隔
static-locations:資源目錄,多個目錄逗號分隔,默認資源目錄為classpath:/meta-inf/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
注意,這個配置會覆蓋spring boot默認的靜態資源目錄,例如如果按示例中配置,則無法再訪問static、public、resources等目錄下的資源了。
四、總結
本文主要給大家分享了spring boot 對靜態資源的處理方式,spring boot 默認可以訪問classpath:/meta-inf/resources/,classpath:/resources/,classpath:/static/,classpath:/public/四個目錄下的靜態資源,我們也可以根據自己的需要進行個性化配置。最后,需要說明一點的是,如果這四個目錄中存在相同名稱的資源,那會優先返回哪個目錄下的資源呢?大家通過static-locations的默認值順序應該能猜到,默認情況下,spring boot會優先返回/meta-inf/resources下的資源。當然,因為我們可以自定義static-locations的值,所以這個優先順序也是可以調整的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/paddix/p/8301331.html