改变默认的静态资源路径,配置静态资源的访问前缀

在SpringBoot中,静态资源放在类路径下:resources/static or /public or /resources or /META-INT/resources 访问方法: 当前项目根路径(也就是/) + 静态资源名 (比如localhost:8080/timg.jpg)

 

 

    如果在Controller类中请求了一个与静态资源名字相同的操作,那么先从Controller开始找(先去找Controller看能不能处理,不能处理的所有请求又交给静态资源处理器,静态资源也找不到则响应404页面)。

    @RequestMapping("/timg.jpg")
    public String show(){
        return "aaa";  //即使静态资源库中有timg.jpg,浏览器还是输出了aaa
    }

 

改变默认的静态资源路径和配置静态资源的访问前缀: 在application.yaml中书写

spring:
  mvc:
    static-path-pattern: /res/**  #配置静态资源的访问前缀,这时要访问就要用 localhost:8080/res/timg.jpg

  resources:
    static-locations: [classpath:/static/]  //改变默认的静态资源路径,这时只在static这个文件夹搜索资源,可以是数组(已经不用了)

 

 

 

 

posted @ 2021-11-11 17:13  Acc22222222  阅读(939)  评论(0)    收藏  举报