zoukankan      html  css  js  c++  java
  • spring boot 静态资源的映射规则 (1) webjars 资源映射

    我们可以在   WebMvcAutoConfiguration 这个类下查找一个方法    addResourceHandlers

      public void addResourceHandlers(ResourceHandlerRegistry registry) {
                if (!this.resourceProperties.isAddMappings()) {
                    logger.debug("Default resource handling disabled");
                } else {
                    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                    if (!registry.hasMappingForPattern("/webjars/**")) {
                        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                    }
    
                    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                    if (!registry.hasMappingForPattern(staticPathPattern)) {
                        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                    }
    
                }
            }

    从这段代码我们可以看出 如果收到    /webjars/**    请求 , 我们会去    classpath:/META-INF/resources/webjars/  这里查找资源文件

    比如 我们 可以 去  https://www.webjars.org/ 这个官网  引入jquery  

    添加依赖到pom.xml 文件下

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.3.1</version>
    </dependency>

    访问 http://localhost:8080/webjars/jquery/3.3.1/jquery.js 

    得到 

  • 相关阅读:
    PKU1008
    PKU 1007
    PKU 3983
    PKU 1005
    PKU1004
    PKU 1003解题
    new.target
    debugger 关键字
    React 高阶组件
    CSS|规范
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/12388807.html
Copyright © 2011-2022 走看看