配置靜態(tài)資源路徑static-locations、static-path-pattern

實際開發(fā)靜態(tài)資源 html、js、圖片 肯定是放在各自文件夾下面的
參考鏈接

一、淺析 static-locations、static-path-pattern

  • spring.mvc.static-path-pattern
    從 WebMvcAutoConfiguration -> WebMvcAutoConfigurationAdapter -> WebMvcProperties 中可以看出默認是 /** ,根據(jù)官網的描述和實際效果,可以理解為靜態(tài)文件URL匹配頭,也就是靜態(tài)文件的URL地址開頭。
private String staticPathPattern = "/**";
  • spring.web.resources.static-locations
    從 WebMvcAutoConfiguration -> WebMvcAutoConfigurationAdapter -> WebProperties -> Resources 中可以看出默認是 "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/",根據(jù)官網的描述和實際效果,可以理解為實際靜態(tài)文件地址,也就是靜態(tài)文件URL后,匹配的實際靜態(tài)文件。
 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
        private String[] staticLocations;
        private boolean addMappings;
        private boolean customized;
        private final WebProperties.Resources.Chain chain;
        private final WebProperties.Resources.Cache cache;

        public Resources() {
            this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
            this.addMappings = true;
            this.customized = false;
            this.chain = new WebProperties.Resources.Chain();
            this.cache = new WebProperties.Resources.Cache();
        }

        public String[] getStaticLocations() {
            return this.staticLocations;
        }

        public void setStaticLocations(String[] staticLocations) {
            this.staticLocations = this.appendSlashIfNecessary(staticLocations);
            this.customized = true;
        }

二、 項目根目錄下新建靜態(tài)文件夾

demo地址

  • SystemData/UserData/Avatar/p1.png
root-static.png

1、 application.properties

  • 分別設置 spring.mvc.static-path-pattern spring.web.resources.static-locations
  • 請注意static-locations中的file:SystemData就是映射本地文件
spring.mvc.static-path-pattern=/SystemData/**
spring.web.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,file:SystemData
proone.png

2、效果展示

showOne.png

三、static 文件下 繼續(xù)分文件

demo 地址

config2.png
error.png

可以訪問:
http://localhost:8080/JS/1.js
http://localhost:8080/Image/1.png
http://localhost:8080/JS/1.html

ok.png

3、 1.js 1.png 1.html 和 2.js 一樣直接訪問

設置 spring.web.resources.static-locations

  • classpath:/static/JS
  • classpath:/static/Image
  • classpath:/static/HTML
spring.web.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,classpath:/static/JS,classpath:/static/Image,classpath:/static/HTML
config.png

3.1 、http://localhost:8080/1.js 、http://localhost:8080/1.pnghttp://localhost:8080/1.html 可以直接訪問了

ok.png

四、需要設置多個地址為靜態(tài)資源目錄

demo地址

這樣的配置,可以說最簡單且粗暴,但是靈活性差一點點:

URL響應地址只能為一項,也就是spring.mvc.static-path-pattern配置只能寫一項。
這意味著,按我上文設置了/SystemData/為URL匹配,就不能設置第二個/resources/這樣的配置為第二靜態(tài)目錄。

many.png

寫一個配置類,實現(xiàn)靜態(tài)資源的文件夾方法很多。比如:
繼承于WebMvcConfigurationSupport父類,并實現(xiàn)addResourceHandlers方法。
引用WebMvcConfigurer接口,并實現(xiàn)addInterceptors方法

1、實現(xiàn)一個一個配置類,并繼承WebMvcConfigurationSupport,實現(xiàn)addResourceHandlers方法,并打上@Configuration注解,使其成為配置類:

package com.example.springboot02staticconfig03.Config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    // System.getProperty("user.dir")  當前程序所在目錄
    static final String IMG_PATH = System.getProperty("user.dir")+"/SystemData/";
    static final String IMG_PATH_TWO = System.getProperty("user.dir")+"/Test/";

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 靜態(tài)資源映射
        registry.addResourceHandler("/SystemData/**").addResourceLocations("file:"+IMG_PATH);
        registry.addResourceHandler("/Test/**").addResourceLocations("file:"+IMG_PATH_TWO);
        super.addResourceHandlers(registry);
    }
}

config.png

2、實現(xiàn)效果

現(xiàn)在我們就來配置。 最終效果很簡單,我想要的效果(兩組同時):

瀏覽器輸入:http://localhost:8080/SystemData/UserData/Avatar/1.png
可以直接訪問項目文件下的:/SystemData/UserData/Avatar/1.png,

瀏覽器輸入:http://localhost:8080/Test/UserData/Avatar/2.png
可以直接訪問項目文件下的:/Test/UserData/Avatar/2.png,

1.png
2.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容