首页处理

静态资源文件夹说完后,我们继续向下看源码!可以看到一个欢迎页的映射,就是我们的首页!

		@Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) 
        {
            WelcomePageHandlerMapping welcomePageHandlerMapping =
             new WelcomePageHandlerMapping(
             new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(),  // getWelcomePage 获得欢迎页
            this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
            welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
            return welcomePageHandlerMapping;
        }



		 private Resource getWelcomePage() {  // 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));
        }
        // 欢迎页就是一个location下的的 index.html 而已
        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.html 页面;被 /** 映射。

比如我访问 http://localhost:8080/ ,就会找静态资源文件夹下的 index.html

所以,当我们访问 http://localhost:8080/ 时,SpringBoot就会在可存放静态资源的文件夹内寻找 index.html 文件。

关于 静态资源的映射规则。
https://blog.csdn.net/norang/article/details/120306786
在这里插入图片描述

参考目录

狂神
https://www.cnblogs.com/hellokuangshen/p/12509918.html
B站 狂神
https://www.bilibili.com/video/BV1PE411i7CV?p=4

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐