背景:
初始搭建好springboot平臺後,發現static目錄下的js.css等靜態資源無法訪問,於是乎,做如下修改:
第一種方案:
直接修改yml配置文件:
#thymeleaf
thymeleaf:
cache: false
prefix: classpath:/templates/
check-template-location: true
suffix: .html
encoding: utf-8
mode: HTML
#這個是關鍵,放開springboot對靜態資源的攔截
mvc:
static-path-pattern: /static/**
第二種方案:
增加java類,告訴springboot對靜態資源的加載路徑
/**
* 配置靜態資源映射
**/
@Component
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 添加靜態資源文件,外部可以直接訪問地址
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//此處還可繼續增加目錄。。。。
}
}