06.SpringBoot的webjars和静态资源映射

使用SpringBoot

  1. 创建Spring Boot应用,选择需要的模块
  2. SpringBoot会将这些默认场景配置好,只需要配置文件中指定少量配置就可以运行起来
  3. 编写业务逻辑代码

xxxxAutoConfiguration:帮助我们给容器中自动配置组件
xxxxProperties:配置类来封装配置文件的内容


Spring Boot对静态资源的映射规则

查看下列文件源码:
在这里插入图片描述

...

     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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
              }

          }
      }

...
1. 所有的webjars/**:都去META-INF/resources/webjars找资源
webjars:以jar包的方式引入静态资源(www.webjars.org)![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702170237215.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)
查看jquery静态文件:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702170440782.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)

从浏览器访问静态文件jquery
http://localhost:8080/webjars/jquery/3.4.1/jquery.min.js
在这里插入图片描述

2. /**:访问当前项目的任何资源
classpath:/META-INF/resources/*
classpath:/resources/*
classpath:/static/*
classpath:/public/*
"/" 当前项目的类路径
例如:在resources/static下新建demo/demo.js
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702172202316.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0NTk4NjY3,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190702172218187.png)

欢迎页:静态资源文件夹下所有的index.html页面
localhost:8080 —>找index.html

3. 所有的**/favicon.ioc都是在静态资源下寻找
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robona

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值