SpringBoot-靜態檔案載入失敗-改寫static路徑
阿新 • • 發佈:2018-12-05
SpringBoot-靜態檔案載入失敗-改寫static路徑
- Spring Boot 對靜態資源對映提供了預設配置
Spring Boot 預設將 /** 所有訪問對映到以下目錄:
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
Spring Boot 預設會挨個從 public resources static 裡面找是否存在相應的資源,如果有則直接返回。
- 自定義靜態資源對映
如果改寫了static的路徑,找不到靜態資源了,別忘記配置靜態資源對映
package core_web.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //將所有/static/** 訪問都對映到classpath:/static/ 目錄下 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } }
- 這樣配置在resoutces下的static會被發現資源
- 測試靜態檔案
static 如果和 resoutces 是平級標籤,如果沒有更改靜態對映,是可以訪問的
隨筆記錄,方便自己學習
chenyb 2018-11-20