1. 程式人生 > 其它 >改變預設的靜態資源路徑

改變預設的靜態資源路徑

在SpringBoot中,靜態資源放在類路徑下:resources/static or /public or /resources or /META-INT/resources

訪問方法: 當前專案根路徑(也就是/) + 靜態資源名 (比如localhost:8080/timg.jpg)

如果在Controller類中請求了一個與靜態資源名字相同的操作,那麼先從Controller開始找(先去找Controller看能不能處理,不能處理的所有請求又交給靜態資源處理器,靜態資源也找不到則響應404頁面)。

    @RequestMapping("/timg.jpg")
    public String show(){
        
return "aaa"; //即使靜態資源庫中有timg.jpg,瀏覽器還是輸出了aaa }

改變預設的靜態資源路徑: 在application.yaml中書寫

spring:
  mvc:
    static-path-pattern: /res/**  #這時要訪問就要用 localhost:8080/res/timg.jpg

  resources:
    static-locations: [classpath:/static/]  //表示只在static這個資料夾搜尋資源,可以是陣列(已經不用了)