SpringBoot配置静态资源映射有两种方式
- 使用配置文件
spring:
web:
resources:
# 开启静态资源映射
add-mappings: true
# 设置静态资源位置,可以设置多个
static-locations: classpath:a/, classpath:/b/
访问路径
localhost:8080/hello.html
SpringBoot会自动去类路径下的a文件夹和b文件夹下寻找hello.html,请求路径不用带上文件夹名称
- 使用配置类
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
// 设置静态资源映射
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
}
}
访问路径
localhost:8080/**/backend/hello.html
SpringBoot会去类路径下的backend文件夹下寻找hello.html
访问路径
localhost:8080/**/front/hello.html
SpringBoot会去类路径下的front文件夹下寻找hello.html
总结
使用配置文件方式,只需要文件匹配
使用配置类方式,需要路径匹配以及文件匹配