springboot designated homepage (static resource import)

1. Where should static resources be placed?

Springboot integrates spring-webmvc, this is all known.
The frame is characterized by automatic assembly.

  1. First look at the WebMvcAutoConfiguration automatic assembly class
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
 	 // 是否手动配置properties或者yaml指定静态资源放置位置
     if (!this.resourceProperties.isAddMappings()) {
    
    
         logger.debug("Default resource handling disabled");
     } else {
    
    
         Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
         CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
		 // webjar 无需关注
         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));
         }

     }
 }

This is the resource location that mvc helped me find.

Click in and out to jump to the ResourceProperties class.

Then, at the point, I found that it is a global character array. It looks like it is empty.

Look at the constructor and assign it!

The array is this thing, isn't it in the resouces directory of the classpath?

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

You can preach the four folders META-INF/resources, static/public/resources under resources, and access the static resources inside through localhost:8080/xxx.

One more is not created.

  • Priority
    resources> static default> public

2. How to automatically display the homepage?

  1. Find the homepage, look for the configuration class related to WebMvcAutoConfiguration mvc
        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");
        }

It is also the same as the array of resources above. Put it under four directories.

As long as index.html is recognized as the home page. Will not 404.

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/108920800