Implementation of five mapping rules for static resources in springboot

Time:2021-1-12

The spring MVC configuration function in springboot is in theWebMvcAutoConfigurationClass,xxxxAutoConfigurationIt helps us to automatically configure components in the container; the shortcut key of idea global search is twiceshift, viewwebMvcAutoConfigurationseewebMvcAuto configuration class

The principle of webmvcaautoconfiguration class needs to be mastered at least a little bit in the future. Here, I just want to see its specific key code. Here, I only give some examples of key code. It’s a headache to watch too much. It doesn’t matter if I can’t understand it. Hahaha, why bother me to skip the source code stage?


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/**")) {
      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));
    }

  }
}

So far, the following parts are the core content of this article! Come on, Ollie, give it to me!!!!

1. Webjars: introducing static resources in the form of jar package

We had one in the web project beforewebappStatic resources are stored in the directory ofspringbootThere is no such file path in, butspringbootIt still has its own rules, and you can see all of them from the code above/webjars/**All of them will goclasspath:/META-INF/resources/webjars/Look for resources in the market. So what is itwebjarsWhat about it?webjarsThat is to sayjarStatic resources are introduced in the form of package;

webjarsOur website: https://www.webjars.org/ [Baidu webjars is OK] after entering, the following operations are performed:

Copy the above dependency into the dependency of Maven project, as follows


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

After that, all / webjars / * * will go to classpath / meta-inf / resources / webjars / to find resources. This path is in the following directory

After removing the tick, the small box on the right is the path in our corresponding classpath / meta-inf / resources / webjars /.

After the above steps are completed, for example, let’s start to visit the jquery.js This resource runs the project directly and then enters it in the address bar http://localhost :8080/webjars/jquery/3.4.1/ jquery.js You can access the jquery.js The page effect is as follows:

Indicates that the static resource access is successful. Yes, the above way is to import the jar package. If we want to use our own JS, CSS and jQuery files, can we? The answer is yes, so there is our second way.

2. / * * access any resources in the current project

For your own static resource files, such as some JS, CSS and jQuery files, springboot reads them from the following paths by default

"classpath:/META‐INF/resources/", 
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/": the root path of the current project

http://localhost:8080/jquery.jsIf there is no code processing, it is equivalent to accessing the static resource folder directlyjquery.js, as follows

3. Home page index.html , mapped by ‘/ * *’

Under the default static resource access path of springboot, add theindex.htmlFile, then accesslocalhost:8080Will automatically jump to thisindex.html。 That is to say, the default home page of springboot is index.html , by“/**“Mapping. As follows:

4. Custom icon/ favicon.ico

In springboot, the icon in ICO format can be placed in the default static resource file path and displayed in favicon.ico Named, the application icon will automatically become the specified icon. All of them/ favicon.ico They are found in the static resource file, as follows:

5. In application.properties Manually configure static resource access path in

stay application.properties Edit the configuration file as follows:

#User defined static resource access path, multiple paths can be specified, separated by commas
spring.resources.static-locations=classpath:/myabc/,classpath:/myhhh

As described in the above note, multiple static resource access paths can be specified and separated by commas. Special attention should be paid to this method: after defining static resources, the default static resource path of springboot will no longer work!

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.