SpringBoot全家桶(2)——全网最详细,涵盖所有知识点,手把手教实战,学完就可上项目!

1. 静态资源导入探究

1.1 底层源码

       静态资源底层源码在WebMvcAutoConfiguration最下面的有个添加静态资源addResourceHandlers()方法

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

       第一个 if 表示如果静态资源被自定义了就直接 return 底层的默认无效了。

       怎么自定义呢 首先在WebMvcAutoConfiguration中找到WebMvcProperties点进去找到this.staticPathPattern = “/**”;一看就知道关于静态资源的在application.properties写入spring.mvc.static-path-pattern=/wxz/非常不推荐自定义。

       第二个if (第一种方式)表示如果patten中有webjars 就会在classpath:/META- INF/resources/webjars/这个路径下找。
       百度搜索webjars 比如要用jquery就导入官方依赖:

<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>jquery</artifactId>
<version>3.7.0</version>
</dependency>

       此时可以直接访问http://localhost8080/webjars/jquery/3.4.1/jquery.js

       第三个if (第二种方式)表示如果符合底层指定的目录,“classpath:/META-INF/resources/”, 跟webjars一样。

       下面三个是指在resources目录下可以创建的文件夹resources或static或public
“classpath:/resources/”,
“classpath:/static/”,
“classpath:/public/”
还有一个 "/
"**

1.2 优先级

       分别在resources,static,public 目录下创建1.jsp 分别输入hello1,hello2,hello3

       结果优先级为 resources > static > public

1.3 首页定制

在WebMvcAutoConfiguration中找到:

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
   
WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
return welcomePageHandlerMapping;
}

private Optional<Resource> getWelcomePage() {
   
String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}

private Resource getIndexHtml(String location) {
   
return this.resourceLoader.getResource(location + "index.html");
}

private boolean isReadable(Resource resource) {
   
try {
   
return resource.exists() && resource.getURL() != null;
<
  • 39
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

坚刚不可夺其志的王先生

如果我写的帮助你了,可以打个赏

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

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

打赏作者

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

抵扣说明:

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

余额充值