欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-07 13:37:28
webmvcautoconfiguration添加资源映射:public void addresourcehandlers(resourcehandlerregistry registry) {...

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引入:

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

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

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

结果是的:

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

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 找

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

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

或者:

/resources/a/b.js找:

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

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

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

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

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

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

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

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

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

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

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

结果:

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

路径:

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

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

上一篇:

下一篇: