springboot~静态资源配置

springboot~静态资源配置

    • springboot的静态资源默认路径
    • 给静态资源请求加前缀
    • 指定静态资源加载的包
    • 欢迎页

springboot的静态资源默认路径

springboot~静态资源配置_第1张图片请求效果见图
springboot~静态资源配置_第2张图片

给静态资源请求加前缀

有时需要用拦截器对某些请求进行拦截后再处理相关的业务代码,此时我们就要对请求进行前缀限制。
下方截图就是配置静态资源请求前缀的方式
springboot~静态资源配置_第3张图片
配置后如果url中不加前缀,就会报404
springboot~静态资源配置_第4张图片
正确写法是http://localhost:8080/res/AA.png

指定静态资源加载的包

spring.web.resources.static-locations: classpath:/目录名/

springboot~静态资源配置_第5张图片

# 静态文件请求匹配方式
spring:
  mvc:
    static-path-pattern: /res/**
  # 修改默认静态寻址资源路径
  web:
    resources:
#      static-locations: classpath:/haha/
     static-locations: [classpath:/haha/]

也可以写成数组的形式,因为spring封装的就是数组,进入到static-locations配置中可以看到

欢迎页

在静态资源目录下创建一个index.html作为系统根路径请求的欢迎页,此时springboot会默认访问静态资源下的index.html文件。
此时不能在配置文件中配置’静态文件请求匹配方式’,不然spring将找不到index.html文件。
springboot~静态资源配置_第6张图片springboot~静态资源配置_第7张图片

你可能感兴趣的:(springBoot,spring,boot,后端,java)