SpringBoot_5(静态资源)

本文详细介绍了SpringBoot中静态资源的映射规则,包括`/webjars/**`、默认静态资源文件的位置、欢迎页和页面图标。此外,还探讨了如何配置静态资源文件夹路径,并简述了SpringBoot不支持动态页面的原因以及推荐使用Thymeleaf模板引擎的原因和使用方法。
摘要由CSDN通过智能技术生成

1.SpringBoot对静态资源的映射规则

       如果我们需用给web项目中添加css/js/html文件的话,我们会发现此时没有webapp目录。

     由于springboot是以jar包的方式打包程序的因此是没有webapp目录的。

     那么我们的css/js/html文件要保存在什么地方啊???

     我们要了解一个Java类“WebMvcAuotConfiguration”,因为与web开发相关的自动配置都是由这个类完成的。

spring-boot-autoconfigure-2.4.0.jar---》META-INF--》spring.factories

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

 

   Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
   CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
   if (!registry.hasMappingForPattern("/webjars/**")) {
      customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)
            .setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
   }
   String staticPathPattern = this.mvcProperties.getStaticPathPattern();
   if (!registry.hasMappingForPattern(staticPathPattern)) {
      customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
            .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
            .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl)
            .setUseLastModified(this.resourceProperties.getCache().isUseLastModified()));
   }
}

WebProperties.java

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = 
{ "classpath:/META-INF/resources/",
 "classpath:/resources/", 
"classpath:/static/",
 "classpath:/public/" };

    1.1静态资源的保存位置所有/webjars/** 

webjars:将需要使用的静态资源打成jar包,我们如果需要就将整个jar导入值本项目就可以使用了。

静态资源打成jar包查看位置:

https://www.webjars.org/

以JQuery为例来使用一下webjars方式

    1.在pom.xml文件中导入JQuery的jar依赖

<!--导入jquery的jar包-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

导人以后的结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值