spring-boot與模板引擎:靜態(tài)資源文件路徑

系列傳送門

spring-boot與模板引擎:靜態(tài)資源文件路徑
spring-boot與模板引擎:修改模板路徑和后綴名
spring-boot與模板引擎:使用metisMenu實現(xiàn)動態(tài)多級菜單

spring-boot與模板引擎:修改模板路徑和后綴名一文中,我們分析了下模板引擎的路徑和后綴名設(shè)置,下面讓我們繼續(xù)分析spring-boot對靜態(tài)資源文件的讀取。

參考:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content

默認讀取的靜態(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/”。

下面我們看個例子:


image.png

那么,網(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/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,502評論 19 139
  • 在前面一章我們用Spring Boot開發(fā)了一個RESTful,本章我們開發(fā)一個WEB界面。 目前Spring官方...
    藍色的咖啡閱讀 61,995評論 0 39
  • application的配置屬性。 這些屬性是否生效取決于對應(yīng)的組件是否聲明為Spring應(yīng)用程序上下文里的Bea...
    新簽名閱讀 5,530評論 1 27
  • 這些屬性是否生效取決于對應(yīng)的組件是否聲明為 Spring 應(yīng)用程序上下文里的 Bean(基本是自動配置的),為一個...
    發(fā)光的魚閱讀 1,482評論 0 14

友情鏈接更多精彩內(nèi)容