實際開發(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)文件夾
- SystemData/UserData/Avatar/p1.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

2、效果展示

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

- 1、默認 2.js 是可以訪問的
http://localhost:8080/2.js
2.png
- 2、但是 1.js 1.png 1.html 都是無法直接訪問的,需要寫完整路徑。
無法訪問:
http://localhost:8080/1.js
http://localhost:8080/1.png
http://localhost:8080/1.html

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

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

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

四、需要設置多個地址為靜態(tài)資源目錄
這樣的配置,可以說最簡單且粗暴,但是靈活性差一點點:
URL響應地址只能為一項,也就是spring.mvc.static-path-pattern配置只能寫一項。
這意味著,按我上文設置了/SystemData/為URL匹配,就不能設置第二個/resources/這樣的配置為第二靜態(tài)目錄。

寫一個配置類,實現(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);
}
}

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,


