SpringBoot如何通过webjars管理静态资源文件夹

 更新时间:2020年10月30日 11:18:42   作者:圣金巫灵  
这篇文章主要介绍了SpringBoot如何通过webjars管理静态资源文件夹,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

WebMvcAutoConfiguration

添加资源映射:

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/ 找资源,
所以就是

http://localhost:8080/webjars/jquery/3.5.1/jquery.js

能访问

/META-INF/resources/webjars/jquery/3.5.1/jquery.js 路径的文件

1) webjars: 以jar包的方式引入静态资源

什么是webjar?

搜索webjar, 可以将jquery用pom引入:

引入, 正好对应这个映射:

结果是的:

2) springboot对静态资源的映射规则:

看代码:

还是

WebMvcAutoConfiguration的这个方法

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));
    }
  }
}

进去:

WebMvcProperties

private String staticPathPattern;
  private final WebMvcProperties.Async async;
  private final WebMvcProperties.Servlet servlet;
  private final WebMvcProperties.View view;
  private final WebMvcProperties.Contentnegotiation contentnegotiation;
  private final WebMvcProperties.Pathmatch pathmatch;

  public WebMvcProperties() {
    this.localeResolver = WebMvcProperties.LocaleResolver.ACCEPT_HEADER;
    this.format = new WebMvcProperties.Format();
    this.dispatchTraceRequest = false;
    this.dispatchOptionsRequest = true;
    this.ignoreDefaultModelOnRedirect = true;
    this.publishRequestHandledEvents = true;
    this.throwExceptionIfNoHandlerFound = false;
    this.logResolvedException = false;
    this.staticPathPattern = "/**";
    this.async = new WebMvcProperties.Async();
    this.servlet = new WebMvcProperties.Servlet();
    this.view = new WebMvcProperties.View();
    this.contentnegotiation = new WebMvcProperties.Contentnegotiation();
    this.pathmatch = new WebMvcProperties.Pathmatch();
  }

addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())) 这里添加了资源的位置

public class ResourceProperties {
  private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
  private String[] staticLocations;
  private boolean addMappings;
  private final ResourceProperties.Chain chain;
  private final ResourceProperties.Cache cache;

  public ResourceProperties() {
    this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
    this.addMappings = true;
    this.chain = new ResourceProperties.Chain();
    this.cache = new ResourceProperties.Cache();
  }

"/**"访问当前项目的任何资源, (静态资源的文件夹) ,如果没人处理,会默认去以下几个文件路径下找[/code]

复制代码 代码如下:
// 静态资源文件夹, 这几个都可以存放静态资源:

classpath:/META-INF/resources/classpath:/resources/"classpath:/static/"classpath:/public/

例如 localhost:8080/a/b.js , 可以到 /META-INF/resources/a/b.js 找

或者:

/resources/a/b.js找:

或者类路径下/static/a/b.js找:

或者/public/a/b.js下找

3)欢迎页面: 静态资源文件夹下的所有index.html页面: 被 /**映射

http://localhost:8080/ 会到以上静态资源文件夹中找index.html页面

源码有变化,我没明白回头再看

结果:

路径:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot配置文件的加载位置实例详解

    SpringBoot配置文件的加载位置实例详解

    springboot采纳了建立生产就绪spring应用程序的观点。 在一些特殊的情况下,我们需要做修改一些配置,或者需要有自己的配置属性。接下来通过本文给大家介绍SpringBoot配置文件的加载位置,感兴趣的朋友一起看看吧
    2018-09-09
  • springcloud干货之服务注册与发现(Eureka)

    springcloud干货之服务注册与发现(Eureka)

    这篇文章主要介绍了springcloud干货之服务注册与发现(Eureka) ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • java字符串比较获取字符串出现次数的示例

    java字符串比较获取字符串出现次数的示例

    java获取一个字符串在整个字符串出现的次数,下面写出我的思路和二个实现方法,大家参考使用吧
    2014-01-01
  • 深入理解注解与自定义注解的一些概念

    深入理解注解与自定义注解的一些概念

    今天给大家带来的文章是注解的相关知识,本文围绕着注解与自定义注解的一些概念展开,文中详细介绍了这些知识,需要的朋友可以参考下
    2021-06-06
  • SpringBoot错误处理机制以及自定义异常处理详解

    SpringBoot错误处理机制以及自定义异常处理详解

    这篇文章主要为大家详细介绍了SpringBoot错误处理机制以及自定义异常处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Mybatis防止sql注入原理分析

    Mybatis防止sql注入原理分析

    这篇文章主要介绍了Mybatis防止sql注入原理分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 详解如何修改idea配置文件位置从C盘更改到D盘

    详解如何修改idea配置文件位置从C盘更改到D盘

    这篇文章主要给大家介绍了关于如何将idea的配置文件从默认的C盘调整到D盘,从而节省C盘使用空间,具有很好的参考价值,希望对大家有所帮助,需要的朋友可以参考下
    2023-10-10
  • SpringCloud创建多模块项目的实现示例

    SpringCloud创建多模块项目的实现示例

    ,Spring Cloud作为一个强大的微服务框架,提供了丰富的功能和组件,本文主要介绍了SpringCloud创建多模块项目的实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • java解析json复杂数据的方法详解

    java解析json复杂数据的方法详解

    这篇文章主要为大家详细介绍了java解析json复杂数据的两种常用方法,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以了解下
    2024-01-01
  • Java实现简易拼图游戏的方法详解

    Java实现简易拼图游戏的方法详解

    这篇文章主要介绍了如何利用Java语言实现简易拼图游戏,帮助大家更好的理解和使用Java开发游戏,感兴趣的朋友可以跟随小编一起学习一下
    2022-05-05

最新评论