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+Mybatis实现登录注册的示例代码

    SpringBoot+Mybatis实现登录注册的示例代码

    这篇文章主要介绍了SpringBoot+Mybatis实现登录注册的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Spring中容器的创建流程详细解读

    Spring中容器的创建流程详细解读

    这篇文章主要介绍了Spring中容器的创建流程详细解读,Spring 框架其本质是作为一个容器,提供给应用程序需要的对象,了解容器的诞生过程,有助于我们理解 Spring 框架,也便于我们“插手”这个过程,需要的朋友可以参考下
    2023-10-10
  • SpringBoot启动时运行特定代码的多种方式小结

    SpringBoot启动时运行特定代码的多种方式小结

    SpringBoot提供了多种方式在应用程序启动时运行特定的代码,包括CommandLineRunner、ApplicationRunner、@PostConstruct、InitializingBean、事件机制和自定义注解等,下面就来具体介绍一下
    2025-01-01
  • 使用mybatis插件PageHelper实现分页效果

    使用mybatis插件PageHelper实现分页效果

    这篇文章主要为大家详细介绍了使用mybatis插件PageHelper实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Java项目如何打包成Jar的实现步骤

    Java项目如何打包成Jar的实现步骤

    一般情况下我们都是使用Java项目直接部署发布,有时需要我们将写好的项目打成jar包,方便后期调用,本文主要介绍了Java项目如何打包成Jar,感兴趣的可以了解一下
    2023-11-11
  • MyBatis如何处理MySQL字段类型date与datetime

    MyBatis如何处理MySQL字段类型date与datetime

    这篇文章主要介绍了MyBatis如何处理MySQL字段类型date与datetime问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • java实现哈夫曼文件解压缩

    java实现哈夫曼文件解压缩

    这篇文章主要为大家详细介绍了java实现哈夫曼文件解压缩,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • Java常用API类之Math System tostring用法详解

    Java常用API类之Math System tostring用法详解

    System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包,Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数,toString() 方法用于返回以一个字符串表示的 Number 对象值
    2021-10-10
  • java变量的声明与赋值分离规范示例

    java变量的声明与赋值分离规范示例

    这篇文章主要为大家介绍了java变量的声明与赋值分离规范示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Java如何读写Properties配置文件(Properties类)

    Java如何读写Properties配置文件(Properties类)

    这篇文章主要介绍了Java如何读写Properties配置文件(Properties类),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05

最新评论