SpringBoot-web static resource mapping rules (six)

About our web development configuration SpringBoot gave us put WebMvcAuotConfiguration this class, we point to open to see. It maps the path static resources is kind of how.

@Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            if (!this.resourceProperties.isAddMappings()) {
                logger.debug("Default resource handling disabled");
                return;
            }
            Integer cachePeriod = this.resourceProperties.getCachePeriod();
            if (!registry.hasMappingForPattern("/webjars/**")) {
                customizeResourceHandlerRegistration(
                        registry.addResourceHandler("/webjars/**")
                                .addResourceLocations(
                                        "classpath:/META-INF/resources/webjars/")
                        .setCachePeriod(cachePeriod));
            }
            String staticPathPattern = this.mvcProperties.getStaticPathPattern();
              //静态资源文件夹映射
            if (!registry.hasMappingForPattern(staticPathPattern)) {
                customizeResourceHandlerRegistration(
                        registry.addResourceHandler(staticPathPattern)
                                .addResourceLocations(
                                        this.resourceProperties.getStaticLocations())
                        .setCachePeriod(cachePeriod));
            }
        }

We can see all / webjars / **, went classpath: / META-INF / resources / webjars / find resources;

webjars: jar package in the manner of the introduction of static resources;

We can go to the official website to see webjars. Http://www.webjars.org/

Jquery import dependence of maven test.

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

(1) at the time of the visit just write the name of the following resources can webjars

localhost:8080/webjars/jquery/3.3.1/jquery.js

Successful visit to the static resources of jquery.

Any resource (2) "/ *" to access the current project, go (static resource files folder) to find the map.

Source Analysis

            if (!registry.hasMappingForPattern(staticPathPattern)) {
                customizeResourceHandlerRegistration(
                        registry.addResourceHandler(staticPathPattern)
                                .addResourceLocations(
                                        this.resourceProperties.getStaticLocations())
                        .setCachePeriod(cachePeriod));
            }
        }
 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/",
 "classpath:/static/", 
"classpath:/public/" };

 

"the CLASSPATH: / META-INF / Resources /",
"the CLASSPATH: / Resources /",
"the CLASSPATH: / static /",
"the CLASSPATH: / public /"
"/": the current root path of the project

For example, we visit localhost: 8080 / nihao will go to static resources folder inside to find nihao

(3) the welcome page; all index.html page static resource folder; by "/ *" Mapping

@Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(
                ResourceProperties resourceProperties) {
            return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
                    this.mvcProperties.getStaticPathPattern());
        }

That is, localhost: 8080 / static resource on the index page to find the folder you can find him automatically configured to match the prefix (index) and the suffix (html) for us.

(4) All ** / favicon.ico are looking at a static resource files

Configure our favorite icon

mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
                        faviconRequestHandler()));
                return mapping;
            }

            @Bean
            public ResourceHttpRequestHandler faviconRequestHandler() {
                ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
                requestHandler
                        .setLocations(this.resourceProperties.getFaviconLocations());
                return requestHandler;
            }

        }

We must be faricon.ico icon's name must be placed in order to be recognized under static resource file folder.

run:

Our icons are displayed out!

 

Guess you like

Origin www.cnblogs.com/xiaoqiqistudy/p/11348475.html