SpringBoot2 静态资源访问问题

1. 静态资源默认位置

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

调整顺序会调整优先级

默认静态资源访问路径
spring.mvc.static-path-pattern=/**

也就是说如果不覆盖默认配置SpringBoot可以很好的工作。

注意:

  1. 使用 @EnableWebMvc 注解会完全覆盖默认行为。
  2. 继承WebMvcConfigurationSupport配置类也会覆盖默认行为。

将不会提供默认的静态资源访问方式,需要自己覆盖,也就是说不能自动设置默认首页,不能自动处理静态路径匹配规则

解决方法:

@Configuration
public class MyWebConfig extends WebMvcConfigurationSupport {
    
    
    @Value("${spring.mvc.static-path-pattern}")
	private String staticPathPattern;
	@Value("${spring.resources.static-locations}")
	private String staticLocations;
	
	@Override
	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
		registry.addResourceHandler(staticPathPattern,"/**").addResourceLocations(StringUtils.split(staticLocations, ","));
	}
}

猜你喜欢

转载自blog.csdn.net/afgasdg/article/details/106474734