SpringBoot-Web static resource mapping rules

SpringBoot course, you can store pages, images and other static resources, but the resources on which these static files, and how to access them? Source can be used to interpret:
first into WebMvcAutoConfiguration class, this class contains SpringMVC web configuration, wherein there is a addResourceHandlers () Method:

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

We can see the first transfer method isAddMappings () to determine whether to do a new map, click through this method:

public boolean isAddMappings() {
    return this.addMappings;
}

This method belongs to the class ResourceProperties, looking at the structure of the class properties:

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
    private String[] staticLocations;
    private boolean addMappings;
    private final ResourceProperties.Chain chain;
    private final ResourceProperties.Cache cache;

    public ResourceProperties() {
        this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
        this.addMappings = true;
        this.chain = new ResourceProperties.Chain();
        this.cache = new ResourceProperties.Cache();
    }

AddMappings attribute set has been found to at construction ResourceProperties true class, the class addResourceHandlers previous methods WebMvcAutoConfiguration directly into else block, and calls the registry addResourceHandler () adds a resource processor, all / webjars / ** have go classpath: to find resources in the / META-INF / resources / webjars / this path, webjars jar package is essentially a way of introducing static resources can be introduced
webjars in jquery dependent on:

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

After introducing dependence found in the outer cladding:
Here Insert Picture Description
back ResourceProperties class, the first class attribute CLASSPATH_RESOURCE_LOCATIONS ResourceProperties:

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

And ResourceProperties class appendSlashIfNecessary () Method:

    private String[] appendSlashIfNecessary(String[] staticLocations) {
        String[] normalized = new String[staticLocations.length];

        for(int i = 0; i < staticLocations.length; ++i) {
            String location = staticLocations[i];
            normalized[i] = location.endsWith("/") ? location : location + "/";
        }

        return normalized;
    }

Show that there are five paths to the storage location can be static resources, is the fifth project root directory:

  1. “classpath:/META-INF/resources/”
  2. “classpath:/resources/”
  3. “classpath:/static/”
  4. “classpath:/public/”
  5. “/”

And priority access to start classpath: / resources / ==> classpath : / static / ==> "classpath: / public /"
after the storage path clear static resources, you can see WebMvcAutoConfiguration class has welcomePageHandlerMapping () Welcome page mapping method:

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

There is a getWelcomePage () method, and then into Point:

private Optional<Resource> getWelcomePage() {
    String[] locations = WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations());
    return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
    return this.resourceLoader.getResource(location + "index.html");
}

getWelcomePage () method which calls getIndexHtml () method to get the index page, it can be concluded: all index pages under static resources / ** mapped in a browser to access localhost: 8080 / after the jump directly to static index.html file in the directory.

If you configured in the global configuration of which:

spring.resources.static-locations=classpath:xxx

The default configuration of all the failures, identified only static resource directory of your configuration.

Published 12 original articles · won praise 3 · Views 246

Guess you like

Origin blog.csdn.net/qq_38599840/article/details/104145145