1 spring.mvc.static-path-pattern
spring.mvc.static-path-pattern=/static/**只有静态资源的访问路径为/static/**时,才会处理请求。比如访问http://localhost:8080/static/a.css,处理方式是据模式匹配后的文件名查找本地文件。按spring.resources.static-locations指定查找的本地文件的位置。
2 spring.resources.static-locations
spring.resources.static-locations自定义Springboot前端静态资源的位置。默认Springboot将从如下位置,按优先级查找静态资源:
spring.resources.static-locations = classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
- 1
3 配置file路径
# 反斜线,windows目录分隔符,前一个\是转义字符,后一个\是目录分隔符。
spring.resources.static-locations=file:\\d:\\dist\\
# 或spring.resources.static-locations=file:///d:/dist/
- 1
- 2
- 3
4 WebMvcConfigurer配置代替
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/static/**")
.addResourceLocations("/file:\\d:\\dist\\/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
参考:https://www.jianshu.com/p/21a7c7ccdec0.