最近发现,可以直接访问到css等,但是不能直接访问到html,后面发现是SpringBoot默认配置了一些目录是可以直接访问的:
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;
public ResourceProperties() {
this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
this.addMappings = true;
this.chain = new ResourceProperties.Chain();
this.cache = new ResourceProperties.Cache();
}
...
}
可以看出默认可以直接访问的是"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"
可以添加一些目录或者直接覆盖使用配置文件(yml/properties),我使用的是application.properties:
# 配置这个是可以让游览器直接访问到html
spring.resources.static-locations: classpath:/static/, classpath:/templates/
配置好后启动项目发现可以直接访问了,但是使用了Thymeleaf的地方就没用了(毕竟访问的是静态的),所以一般情况下这个配置不写吧,知道就行了。emmm。