SpringBoot------Web开发:首页加载以及图标如何定制(十)

SpringBoot------Web开发:首页如何定制

同样是在WebMvcAutoConfiguration配置类的源码中

        @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));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }


        private Resource getWelcomePage() {
    
    
            String[] var1 = this.resourceProperties.getStaticLocations();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
    
    
                String location = var1[var3];
                Resource indexHtml = this.getIndexHtml(location);
                if (indexHtml != null) {
    
    
                    return indexHtml;
                }
            }

            ServletContext servletContext = this.getServletContext();
            if (servletContext != null) {
    
    
                return this.getIndexHtml((Resource)(new ServletContextResource(servletContext, "/")));
            } else {
    
    
                return null;
            }
        }

        private Resource getIndexHtml(String location) {
    
    
            return this.getIndexHtml(this.resourceLoader.getResource(location));
        }

        private Resource getIndexHtml(Resource location) {
    
    
            try {
    
    
                Resource resource = location.createRelative("index.html");
                if (resource.exists() && resource.getURL() != null) {
    
    
                    return resource;
                }
            } catch (Exception var3) {
    
    
            }

            return null;
        }

getWelcomePage()方法中能看出:读取this.resourceProperties.getStaticLocations();地址。
而Resource对象的初始化时,this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
CLASSPATH_RESOURCE_LOCATIONS时源码中定义的常量数组。

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

和上一篇学习时静态资源放置的地址一致。for循环这个数组调用this.getIndexHtml()方法。

Resource indexHtml = this.getIndexHtml(location);

this.getIndexHtml()方法。

		private Resource getIndexHtml(String location) {
    
    
            return this.getIndexHtml(this.resourceLoader.getResource(location));
        }

	
 		private Resource getIndexHtml(Resource location) {
    
    
            try {
    
    
                Resource resource = location.createRelative("index.html");
                if (resource.exists() && resource.getURL() != null) {
    
    
                    return resource;
                }
            } catch (Exception var3) {
    
    
            }

            return null;
        }

index页面放在SpringBoot默认的几个路径下都能够访问到。:http://localhost:8080/index.html
http://localhost:8080/也可以访问到。
在这里插入图片描述

新版SpringBoot设置默认图标已经废弃

在这里插入图片描述
在静态文件目录下直接放置一个favicon.ico图片,并且把默认图标关闭设置为false,能够在页面显示自己设置的图标,但是在新版本SpringBoot中此功能已经被舍弃。

猜你喜欢

转载自blog.csdn.net/cz_chen_zhuo/article/details/117122144
今日推荐