SpringBoot 2.0 静态资源访问
SpringBoot 2.0 静态资源访问
1. 静态资源目录
1.1 springboot 静态资源目录
静态资源访问目录,默认共有5个
1. classpath:/META-INF/resources/
2. classpath:/resources/
3. classpath:/static/
4. classpath:/public/
5. src/main/webapp 目录访问,默认需要配置
1.2 springboot配置源码
package org.springframework.boot.autoconfigure.web;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.http.CacheControl;
/**
* Properties used to configure resource handling.
*
* @author Phillip Webb
* @author Brian Clozel
* @author Dave Syer
* @author Venil Noronha
* @author Kristine Jetzke
* @since 1.1.0
*/
@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/" };
也可以在application.yml中进行配置:
spring:
resources:
static-locations: classpath:/resources, classpath:/public
1.3 在IDEA中配置"/"目录
webapp默认不支持。springboot默认是直接集成tomcat容器,原理是将tomcat的默认工作目录,指定为tomcat工作目录
方法一: 将工作目录指到本地实际存储的目录
方法二: 配置映射目录
方法三: 打成war放入tomcat,看一下目录结构就明白了
2. 目录说明
2.1 访问优先级
目录的访问级别,排前的为优先:
目录 | 访问优先级 |
---|---|
classpath:/META-INF/resources/ | 最高 |
classpath:/resources/ | - |
classpath:/static/ | - |
classpath:/public/ | - |
src/main/webapp | 最低 |