一、先说默认的静态资源路径
下面截取了一段ResourceProperties类的源码,可以看到定义了一个final数组CLASSPATH_RESOURCE_LOCATIONS并初始化了一些值,这些值就是默认的静态资源路径,这些文件夹下的文件可以直接访问。
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
}
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
这几个路径分别对应项目中的如下文件夹

图1
二、覆盖默认配置 or 增加静态资源路径
目前我只知道两种覆盖默认配置的方式
Ⅰ、使用 spring.resources.static-locations配置
其中的classpath:/my-path/是自定义的路径,其他的是SpringBoot默认的路径,当然也可以不加默认路径。
application.properties如下:
# 项目路径
server.servlet.context-path=/test-demo
# 静态资源配置
spring.resources.static-locations=
classpath:/META-INF/resources/,
classpath:/resources/,
classpath:/static/,
classpath:/public/,
classpath:/templates/,
classpath:/my-path/
这样配置,除了可以直接访问spring默认的路径下的静态资源,也可以直接访问classpath:my-path下的静态资源。
Ⅱ、使用spring.mvc.static-path-pattern配置
aplication.properties如下:
# 项目路径
server.servlet.context-path=/test-demo
# 静态资源配置
spring.mvc.static-path-pattern=/static/**
敲黑板&#