zoukankan      html  css  js  c++  java
  • SpringBoot 之静态资源

    boot 的默认的静态资源有多个, 由 ResourceProperties 配置了默认值:

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

    具体的配置工作是WebMvcAutoConfiguration的ResourceChainResourceHandlerRegistrationCustomizer完成的。

    其先后顺序, 正是静态资源的查找顺序。 也就是说, 如果出现了多个同名的静态资源, 那么以 前面找到的为准。 假设下面的目录结构:

    resources
        META-INF
            resources
                subDir
                    aaaa.html
                aa.html
                lk.jsp
        public
            aa.html
        resources
            aa.html
        static
            aa.html
            err.html
        templates
            aa.html
            aaxx.html
            errorrr.html
        application.properties

    我们访问 aa.html , 且假设没有 /aa或 /aa.html 的映射, 那么 META-INF.resources 下面的 aa.html 会被首先访问,其次是 resources目录下面的,再次是 static, 最后是 public。

    这里的静态资源,可以是 html , js,css, pnp 等图片, pdf, excel , doc,ppt等,也甚至可以是jsp, 但是, 我测试过, 如果把jsp 当作静态资源,直接访问的话, 那么 浏览器是识别不了的,那么就会出现一个下载框(或者直接下载)。 总之,只要我们的请求路径是 上面RESOURCE_LOCATIONS 之一开头的,但是它和我们的配置,比如@RequestMapping,冲突了。 那么boot 优先使用 显式的@RequestMapping配置。

    需要特别特别注意的是, 如果 静态资源的裸文件名,也就是文件去后缀后的文件名,如果 和某个 @RequestMapping 冲突了, 那么我们就无法直接访问这个 静态资源了!!!因为@RequestMapping 是可以忽略后缀匹配的。  这个时候,我们实际是访问了一个 @RequestMapping, 响应结果当然是由这个 方法来确定的。

    boot, 当然也是额外提供了 对这个 静态资源位置的配置的:

    spring.mvc.static-path-pattern=    # 默认值为 /**

     

    # 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
    spring.resources.static-locations= 这里设置要指向的路径,多个使用英文逗号隔开,

    使用 spring.mvc.static-path-pattern 可以重新定义pattern,如修改为 /myres/** ,则访问static 等目录下的fengjing.jpg文件应该为 http://localhost:8080/myres/fengjing.jpg ,修改之前为 http://localhost:8080/fengjing.jpg 。 spring.mvc.static-path-pattern 的格式必须是 xx/* xx/**  /xx/* /xx/** , 我这里已经说的很明白了吧, 只有4种, 其实就两种, xx/*  /xx/* 等价,是一个意思, 貌似boot 会自动给第一个路径补一个斜杠,如果没有的话(推荐使用斜杠开头的方式, 这样会让boot 少走一些“弯路”)。 而xx/* xx/** 的区别是, 一级目录,和多层级目录的区别了, 是 ant 目录格式。 当然, 我们也可以去掉星号,配置成 /xx  /xx/yy, 这个是什么? 有些怪异了,我也迷糊了, 我也就不明白了。


    注意 spring.mvc.static-path-pattern 只可以定义一个,目前不支持多个逗号分割的方式。

    使用 spring.resources.static-locations 可以重新定义 pattern 所指向的路径,支持 classpath: 和 file: (上面已经做过说明) ,如果我们不写 classpath或file 前缀, 那么boot 是识别不了的!!! 而且呢, boot 也是不会报错的! 这个真的有些坑啊! 另外, 记住,配置 spring.resources.static-locations 的值的先后顺序就是查找的顺序!!

    另外,spring.resources.static-locations还可以配置成空,坑!! 比如我不小心配置下面这个样, 结果boot 也生效了, 而且没报错!但这样之后我就访问不了任何静态资源了。。坑!!:

    spring.resources.static-locations=

    对静态文件的处理(主要是定位工作), 是org.springframework.web.servlet.resource.ResourceHttpRequestHandler 完成的。

    ResourceHttpRequestHandler 完全是spring mvc 的范畴了。 它的作用就是找到那个url 对应的资源(通常是html/css/js之类的)然后检查是否被改变,如果未改变则直接返回304.改变则返回新内容。

    除此之外, 我们可以编程方式添加 静态资源位置:

    @Configuration
    public class MyWebAppConfigurer 
            extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/");
            super.addResourceHandlers(registry);
        }
    
    }

    参考:

    http://blog.csdn.net/catoop/article/details/50501706 

  • 相关阅读:
    delete
    What's an Aggregate Root?
    Mediator Pattern中介者模式
    Domain events: design and implementation
    "ISerializable" should be implemented correctly
    Package version is always 1.0.0 with dotnet pack
    CA1005: Avoid excessive parameters on generic types
    Event Sourcing pattern
    Command and Query Responsibility Segregation (CQRS) pattern
    Implementing event-based communication between microservices (integration events)
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/7968516.html
Copyright © 2011-2022 走看看