系列傳送門
spring-boot與模板引擎:靜態(tài)資源文件路徑
spring-boot與模板引擎:修改模板路徑和后綴名
spring-boot與模板引擎:使用metisMenu實現(xiàn)動態(tài)多級菜單
在spring-boot與模板引擎:修改模板路徑和后綴名一文中,我們分析了下模板引擎的路徑和后綴名設(shè)置,下面讓我們繼續(xù)分析spring-boot對靜態(tài)資源文件的讀取。
默認讀取的靜態(tài)資源路徑
查看包名org.springframework.boot.autoconfigure.web下的ResourceProperties源碼:
public class ResourceProperties implements ResourceLoaderAware {
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[]{"/"};
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
private static final String[] RESOURCE_LOCATIONS;
......
......
}
可以看到,spring-boot默認讀取的資源資源文件根路徑有4個:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
在啟動spring-boot項目的時候,我們可以看到下面日志:
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
其中 [/webjars/**] ,映射的目錄為 classpath:/META-INF/resources/webjars/。
其中 [/**] ,映射的目錄為 classpath:/resources/,classpath:/static/和classpath:/public/。
其中 [/**/favicon.ico] ,映射的路徑為 classpath:/resources/favicon.ico,或classpath:/static/favicon.ico,或classpath:/public/favicon.ico。
需要注意的是,如果是用Intellij IDEA開發(fā),項目默認生成的Resources目錄,不是上面說的“classpath:/resources/”,這個Resources目錄是直接指向“classpath:/”的。Resources目錄下新建一個“resources”文件夾,此時“resources”文件夾的路徑才是“classpath:/resources/”。
下面我們看個例子:

那么,網(wǎng)頁中,我們應(yīng)該怎么寫,才能訪問到bootstrap.min.js這個靜態(tài)資源呢?
答案如下:
<script src="/js/bootstrap.min.js"></script>
手動配置靜態(tài)資源路徑
spring的包中提供了一個抽象類:WebMvcConfigurerAdapter:
......
......
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
......
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
......
}
......
......
我們可以自己創(chuàng)建一個類,繼承該類,并重寫addResourceHandlers方法:
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 MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
// 將META-INF/resources/test目錄下的所有文件,映射到classpath:/static/路徑下
registry.addResourceHandler("/test/**").addResourceLocations("classpath:/static/");
}
}
除了上面的方式外,也可以通過配置Application.properties來設(shè)置:
# 默認為/**,此時在html里面訪問classpath:/static/js/test.js,需要這樣寫:/js/xxx.js
# 配置為/static/**,此時在html里面訪問classpath:/static/js/test.js,需要這樣寫:/static/js/xxx.js
spring.mvc.static-path-pattern=/**
# 默認為classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
# 手動配置多個,需要用逗號分隔,比如classpath:/js/,classpath:/css/,此時在html里面訪問classpath:/js/test.js,需要這樣寫:/test.js
spring.resources.static-locations=classpath:/js/,classpath:/css/