SpringBoot对静态资源的映射规则

在WebMvcAutoConfiguration类中有相对应的方法addResourceHandlers

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();
       //静态资源的映射是在/webjars/目录下的
         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));
         }
       }
      }

		//配置欢迎页面
		@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;
        }

1)、所有的/webjars/**,都要去classpath:/META-INFO/resources/webjars/目录下找资源;

webjars: 以jar包的方式引入静态资源

例如引入jQuery,直接到webjars官网,复制相关依赖,粘贴到pom.xml中就可以使用jQuery的功能。

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.4.1</version>
</dependency>

在这里插入图片描述

  1. 、/**,当前项目的静态资源放在以下的文件夹中是有效的,SpringBoot会在特定的静态文件夹下找映射

classpath:/META-INf/resources/

classpath: /resources/

classpath:/static

classpath:/public

/: 指的是当前项目的根目录。默认情况下,java是源码的根目录,resources是配置文件的根目录

在这里插入图片描述

3)、欢迎页面其实就是在浏览器中输入localhost:8080/时出现的页面,这个我们都知道是index.html/index.jsp页面,这个就是欢迎页面

4)、所有在静态资源文件夹下被命名为favicon.ico的图片都被作为当前项目的网页图标

posted @ 2020-02-02 21:05  起个名字都这么男  阅读(177)  评论(0编辑  收藏  举报