天天看點

Springboot 靜态資源路徑配置 執行個體介紹

這裡主要介紹使用yml去配置靜态資源路徑,會由簡單慢慢到詳細地去介紹怎麼使用yml來配置靜态資源

示例:

單獨配置了靜态資源的通路路徑  ,這個配置項相當于重新定義該項目的靜态資源檔案夾路徑

spring:
  resources:
    static-locations: classpath:html/      

然後咱們在resources檔案夾路徑下,建立檔案夾html ,放入一個簡單的頁面testPage.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>hello~</h1>

</body>
</html>      

目錄路徑: 

Springboot 靜态資源路徑配置 執行個體介紹

 接下來,我們編寫一個簡單的接口,通路我們上面配置的靜态資源 testPage.html:

@Controller
public class TestController {
    @GetMapping("/test")
    public String test() {
        System.out.println("方法已經通路到了!");

        return "testPage.html";
    }

}      

結果:

Springboot 靜态資源路徑配置 執行個體介紹

可以看到我們的頁面是通路成功了,在這時候我們把一張圖檔  testImage.jpg 放到這個剛剛建立的靜态資源檔案夾 html裡面:

Springboot 靜态資源路徑配置 執行個體介紹

再重新開機 ,通路​​http://localhost:8091/testImage.jpg​​ :

(可以看到這個靜态資源讀取是肯定沒有問題了)

Springboot 靜态資源路徑配置 執行個體介紹

 記下來,我們一起來優化一下接口通路html的編碼,也就是不需要在傳回的内容裡面加 .html  :

Springboot 靜态資源路徑配置 執行個體介紹

那麼我們就需要用到新的配置項,前字尾的配置 :

spring:
  resources:
    static-locations: classpath:html/
  mvc:
    view:
      suffix: .html
      prefix:      

因為我們這裡html在resources檔案夾下面的第一級目錄,是以不需要配置字首,隻配置字尾 .html   

然後我們把接口改成:

@GetMapping("/test")
    public String test() {
        System.out.println("方法已經通路到了!");
        return "testPage";
    }      

重新開機項目,通路接口,結果:

Springboot 靜态資源路徑配置 執行個體介紹

OK,這樣已經在通路靜态資源上比較完善了。

接下來,還需要講一個配置項,靜态資源尋址攔截比對(規則)  static-path-pattern:,

spring:
  resources:
    static-locations: classpath:html/
  mvc:
    view:
      suffix: .html
      prefix: 
    static-path-pattern: /JCccc/**      

 可以看到我添加的規則是 /JCccc/**  ,這個意思是,就算項目在找靜态資源的時候,優先回去校驗通路路徑是否符合規則,

隻有符合規則的路徑,才繼續幫它去尋找相關的靜态資源。

那麼,我們添加完這個配置項後,我們重新開機項目,通路接口:

Springboot 靜态資源路徑配置 執行個體介紹

可以看到,其實接口已經通路到了,但是在最後傳回html頁面時,比對規則沒有符合,是以傳回了 404.

那麼我們需要做出對應的更改:

@GetMapping("/test")
    public String test() {
        System.out.println("方法已經通路到了!");
        return "JCccc/testPage";
    }      

OK,重新開機項目,通路接口:

Springboot 靜态資源路徑配置 執行個體介紹

到了這裡,其實關于springboot靜态資源的yml配置介紹,其實都玩了一遍。

那麼最後,試着在的testPage.tml頁面,加上一個在該頁面上通路靜态資源圖檔,作為最後一個小例子:

我們先在static檔案夾下,建立檔案夾image,裡面放入圖檔image100.jpg:

Springboot 靜态資源路徑配置 執行個體介紹

然後在yml裡面添加該路徑為靜态資源路徑:

spring:
  resources:
    static-locations: classpath:html/,classpath:static/
  mvc:
    view:
      suffix: .html
      prefix:
    static-path-pattern: /JCccc/**      

然後修改下testPage.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello~</h1>
<img src="JCccc/image/image100.jpg" height="1920" width="1080"/></body>
</body>
</html>      

這裡需要主要注意的,在頁面裡面通路項目靜态資源,規則也是一樣的,我們設定了比對規則JCccc,是以也必須遵守這個規則。

接口還是沒變:

@GetMapping("/test")
    public String test() {
        System.out.println("方法已經通路到了!");
        return "JCccc/testPage";
    }      

重新開機項目,通路接口,可以看到靜态頁面,靜态資源都是通路沒有問題的:

Springboot 靜态資源路徑配置 執行個體介紹

OK,這Springboot 靜态資源路徑配置  執行個體介紹 就到此吧。

還簡單補充一個,

我們在電腦本地建立一個檔案夾,放張圖檔,作為靜态資源檔案夾通路:

Springboot 靜态資源路徑配置 執行個體介紹
Springboot 靜态資源路徑配置 執行個體介紹

繼續閱讀