1.静态资源目录
默认访问类路径下 called /static
(or /public
or /resources
or /META-INF/resources
)这些包下的静态资源
访问:当前项目路径+静态资源名
默认静态映射 /**
请求进入 先去controller看是否能够处理 ,然后把所有不能处理的请求交给静态资源处理器。
改变静态默认资源路径
spring.web.resources.static-locations=classpath:/xx/**
2.静态资源访问的前缀
默认无前缀
spring.mvc.static-path-pattern=/resources/**
3.静态资源管理
启动时
WebMvcAutoConfiguration类被加载
@EnableConfigurationProperties({ WebMvcProperties.class,
org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
将配置文件中的值注入到类中
WebMvcProperties == spring.mvc ResourceProperties== spring.resources
然后在该方法中进行映射
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//当isAddMappings为false时 不开启资源映射
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
//webjars资源映射
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
//静态资源映射
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
registration.addResourceLocations(resource);
}
});
}
进入getStaticLocations()方法 细看属性默认值
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;