SpringBoot in five static resource mapping rules

@

SpringMVC SpringBoot configuration capabilities are in the WebMvcAutoConfigurationclass, xxxxAutoConfigurationit is to help us to automatically configure the component to the container; shortcut key idea is twice the global search shift, view webMvcAutoConfigurationview webMvcautomatic configuration class
Here Insert Picture Description
principle WebMvcAutoConfiguration class even after at least somewhat grasp, and this article just to see its specific key code here only mentioned some of the key code and more looking at had a headache, it does not matter ha ha ha can not read the source code to skip the stage, inviting why bother?

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));
        }

    }
}

Well, here, the following is part of the core content of this article! Orly fight vigorously to give! ! ! !

1, webjars: jar package in the manner of the introduction of static resources

Before we have a project in the web webappdirectory to store static resource, although springbootthere is no path to this file, but springbootstill has its own rules, it can be seen from the code above all /webjars/**, we will go classpath:/META-INF/resources/webjars/in looking for resources. So what is webjarsit? webjarsThat it is, in a jarway to introduce the package of static resources;

webjarsOfficial website: https://www.webjars.org/ [row] Direct Baidu webjars also, after entry, as follows:
Here Insert Picture Description
the above-dependent dependent copy into maven project to, the following

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

After all / webjars / ** will go to the classpath: / META-INF / resources / webjars / in looking for resources, this path is in the following directory
Here Insert Picture Description
Here Insert Picture Description
to remove the hook after the right of the small box is our corresponding classpath: / META-INF / resources / webjars / in the path.

After the above steps are completed, such as we begin to look at jquery.js access this resource projects run directly jQuery, and then enter in the address bar http: // localhost: 8080 / webjars / jquery / 3.4.1 / jquery.js can access the corresponding path jquery.js file, the page effect is as follows:
Here Insert Picture Description
Description access static resources success. Is above the jar package in this way is to import the way, if we have to use some of their own JS, CSS, jQuery file can you do? The answer is yes, so there is a second way appears below us.

Any resources 2, / ** access the current project

If you own a static resource files, such as some of their own JS, CSS, jQuery file, SpringBoot default is read from the following paths

"classpath:/META‐INF/resources/", 
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

http://localhost:8080/jquery.jsIf you do not deal with any code, go directly to the equivalent of static resources folder inside access jquery.js, as follows
Here Insert Picture Description

3 Home index.html, is "/ **" maps

The default name is added under static resource access path in SpringBoot index.htmlfile, access localhost:8080will automatically jump to this index.html. That SpringBoot default home side as index.html, is " /**" maps. as follows:Here Insert Picture Description

4, custom icons / favicon.ico
in SpringBoot, you can put an icon ico format in the default static resource file path and name in order to favicon.ico, the application icon will automatically become the designated icon. All
/favicon.ico will find in a static resource files; as follows:

Here Insert Picture Description

5, in application.properties configure a static resource access path manually

In application.properties edit the configuration file as follows:

# 自定义静态资源访问路径,可以指定多个,之间用逗号隔开
spring.resources.static-locations=classpath:/myabc/,classpath:/myhhh

Note, as described above, as a custom static resource access path can specify multiple, separated by commas between, in which particular attention in this way: Custom static resources, SpringBoot default path resources are not static longer work !

If this article there is a little bit of help to you, then please point a chant praise, thank you ~

Finally, if there is insufficient or is not correct, please correct me criticism, grateful! If you have questions please leave a message, the absolute first time to reply!

I welcome you to focus on the public number, there are some java learning materials and a large wave of java e-books, such as Zhou Zhiming teacher depth java virtual machine, java programming ideas, the core technology volume, Westward design patterns, java concurrent programming combat ... .. is a java Bible, do not say fast car on Tomcat, ye who go! The main thing is to explore technology, yearning technology, the pursuit of technology, said good pots Friends is coming Oh ...

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/yichunguo/p/12115550.html