zoukankan      html  css  js  c++  java
  • 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的图片都被作为当前项目的网页图标

  • 相关阅读:
    向量
    3D坐标系
    Unity坐标系详解
    5G 系统流程系列:AF 的 Traffic Routing Control 以及 UP 路径管理增强
    Git 合并冲突
    撤销 git commit
    Redis NoSQL
    Netflow/IPFIX 流量收集与分析
    Nokia 5GC 产品概览
    通过 OpenAPI 部署 Npcf_PolicyAuthorization-PostAppSessions API Service
  • 原文地址:https://www.cnblogs.com/dataoblogs/p/14121967.html
Copyright © 2011-2022 走看看