问题描述:
我创建一个MyConfiguration用于配置扩展springmvc功能

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //添加一个视图映射

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index.html").setViewName("index");
    }
}

同时在static文件夹中存在index.html
捕获.PNG
在输入路径
http://localhost:8080/
访问的是templates路径下的index.html???
问题分析;
我先将templates的index.html改名,然后访问http://localhost:8080/,毫无疑问访问的是static/index.html

@Bean
        public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
            WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
            welcomePageHandlerMapping.setInterceptors(this.getInterceptors());
            return welcomePageHandlerMapping;
        }

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

默认在静态路径下的index.html
在加载welcome路径时,默认往资源路径上添加index.html,在遇到viewController后自然就访问的是templates下的index.html