静态资源路径
springboot默认的静态资源路径有:
resources下的static,public,resource,META-INF/resources。
平时创建项目的时候只有static文件夹,但是可以自己手动创建。
我们可以看看源码:
我们首先创建一个yaml文件,或者properties文件
输入以下代码,这个代码是设置静态资源路径的。
resources:
static-locations:
点开查看源码:
public class ResourceProperties {
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 final ResourceProperties.Chain chain;
private final ResourceProperties.Cache cache;
可以看到这里指定了静态资源路径。
我们可以再yaml文件里配置自己想要的路径:
web:
resources:
static-locations:
classpath: /abc/
我这里就在resources下面新建了一个名为abc的文件夹,把静态资源放在里面,就可以实现访问了。
但是注意这里有两种写法:
spring.resources.static-locations=classpath:/abc/
web.resources.static-locations=classpath:/abc/
这两种写法都是对的,但是根据版本不同,自己选择。我的2.7版本的springboot就不能用上面的,只能用web开头。
静态资源访问加前缀
这个更简单:
spring:
mvc:
static-path-pattern: /res/**
这样就行了这样的话,我们访问静态资源必须加上res的前缀。
最后看看结果
访问我的静态资源。