12.springboot与web开发

springboot对静态资源的映射规则:

@ConfigurationProperties(
    prefix = "spring.resources",
    ignoreUnknownFields = false
)
public class ResourceProperties {
    可以设置和静态资源有关的参数(缓存时间等)--->和下面的:Duration cachePeriod = this.resourceProperties.getCache().getPeriod();对应起来!
}
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/**")) {
            //所有的/webjars/**,都去classpath:META-INF/resources/webjar找资源
            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));
        }

    }
}

1.所有的/webjars/**(所有请求),都去classpath:META-INF/resources/webjar找资源

webjars:以jar包的形式去引入静态资源
jquery的webjar目录结构如下:和代码中的对应上了

访问路径:localhost:8080/webjars/jquery/3.5.1/jquery.js可以访问到jquery的webjar中的内容!
<!--引入jquery的webjar-->  在访问时只需要写webjars下面的资源的名称即可!
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.5.1</version>
</dependency>

2."/**"访问当前项目的任何资源:(静态资源的保存文件夹)

分析源码得知:当访问当前项目的任何资源时:会去当前类路径下的以下文件夹进行查找:
"classpath:/META-INF/resources/", 
"classpath:/resources/", 
"classpath:/static/", 
"classpath:/public/"
"/":当前项目的根路径

当访问路径是:localhost:8080/abc=====>去静态资源文件夹中找abc
以下路径可以存放自定义静态资源!

3.欢迎页的映射

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

欢迎页:静态资源文件夹下的所有index.html页面;被"/**"映射
当访问路径是:localhost:8080/  会去静态文件资源夹中找index.html

 

posted @ 2022-05-10 21:40  努力的达子  阅读(31)  评论(0编辑  收藏  举报