springboot学习笔记03——静态资源导入与首页探究

1.静态资源处理

SpringBoot中,SpringMVC的web配置都在 WebMvcAutoConfiguration 这个配置类里面,而当程序识别出用户发出的URL请求的是静态资源而不是控制器的话,这时便会调用WebMvcAutoConfiguration类下的addResourceHandlers方法,该方法具体如下:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    if (!this.resourceProperties.isAddMappings()) {
        // 已禁用默认资源处理
        logger.debug("Default resource handling disabled");
        return;
    }
    // 缓存控制
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    // webjars 配置
    if (!registry.hasMappingForPattern("/webjars/**")) {
        customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
                                             .addResourceLocations("classpath:/META-INF/resources/webjars/")
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
    // 静态资源配置
    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    if (!registry.hasMappingForPattern(staticPathPattern)) {
        customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
                                             .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                                             .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    }
}

addResourceHandlers方法中定义了两种不同的静态资源映射规则。

1.1 webjars
第一种静态资源映射规则是对于 “ /webjars/** ”的URL,程序会去classpath:/META-INF/resources/webjars/ 找对应的资源

1.2 /**
第二种静态资源映射规则是对于“ /** ”的URL,程序会分别去找以下四个目录下的文件,访问的优先级正好也是从高到低,即优先访问"classpath:/META-INF/resources/" 文件夹下的文件

“classpath:/META-INF/resources/”
“classpath:/resources/”
“classpath:/static/”
“classpath:/public/”

这点结合addResourceHandlers方法以及resourceProperties类中的下述代码便可得出结论


// 进入方法
public String[] getStaticLocations() {
    return this.staticLocations;
}
// 找到对应的值
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
// 找到路径
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 
    "classpath:/META-INF/resources/",
   "classpath:/resources/", 
    "classpath:/static/", 
    "classpath:/public/" 
};

此外,如果想要自定义静态资源路径,可以在配置文件中application.properties中进行配置,如

spring.resources.static-locations=classpath:/coding/,classpath:/xu/

那么,当程序就会分别去找“classpath:/coding/”以及“classpath:/xu/”目录下的静态资源文件。

2.首页处理

首页处理与静态资源处理的相关源码都在WebMvcAutoConfiguration这个配置类里面,在WebMvcAutoConfiguration配置类中,找到以下代码:


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

private Optional<Resource> getWelcomePage() {
    String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
    // ::是java8 中新引入的运算符
    // Class::function的时候function是属于Class的,应该是静态方法。
    // this::function的funtion是属于这个对象的。
    // 简而言之,就是一种语法糖而已,是一种简写
    return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
// 欢迎页就是一个location下的的 index.html 而已
private Resource getIndexHtml(String location) {
    return this.resourceLoader.getResource(location + "index.html");
}

上述代码主要的内容是,欢迎页面就是位于静态资源文件夹下面的index.html页面,比如我访问 http://localhost:8080/ ,程序就会找静态资源文件夹下的 index.html页面返回给用户。
例如在类路径下创建一个名为resources的文件夹,并在该文件夹中创建index.html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>

随后启动程序,访问 http://localhost:8080,便能看到下面的页面:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Xulidanga

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

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

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

打赏作者

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

抵扣说明:

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

余额充值