springboot关于静态资源的配置如下:
查看源码:自动配置类WebMvcAutoConfiguration、ResourceProperties、WebMvcProperties
1、静态资源目录配置
spring.resources.staticLocations
默认值:classpath:/META-INF/resources/,
classpath:/resources/,
classpath:/static/,
classpath:/public/
优先级:前面大于后面
2、静态资源访问模式
spring.mvc.staticPathPattern
默认值:/**
实例:
我们在src/main/resources下建立static文件夹
注意:访问路径不要static,springboot自动到默认的静态资源目录下找!
spring.mvc.staticPathPattern默认为/*,会拦截所有请求,如果找不到资源再去寻找controller,所以我们可以在application.properties覆盖默认配置spring.mvc.staticPathPattern=/static/*,只有static前缀的url才去访问静态资源目录!
这里访问路径加上上下文:springboot,可以在application.properties中配置:
#server,内嵌tomcat配置信息
#端口,默认值8080
server.port=8080
#应用上下文,默认值/
server.servlet.contextPath=/springboot