栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot加载前端首页index.html

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot加载前端首页index.html

本文以springboot2.2.x为例,讲述框架如何加载前端默认首页资源index.html。

定位到源码:WebMvcAutoConfiguration,该类负责加载index.html资源,具体负责为:welcomePageHandlerMapping注入bean,可以很清楚的看到最终搜索index资源的方法为getIndexHtml,该方法会默认对五个地址进行资源查看,查找的顺序是 classpath:/META-INF/resources/ ;classpath:/resources/;classpath:/static/;classpath:/public/;/;

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

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

​

并且一定要注意项目正常启动后的 项目target(编译好的)文件夹中是否有编译html文件,作者之前因项目中的pom.xml配置build/resources中没有将html资源纳入导致springboot框架找不到页面资源定位了很久原因。以下就是当时定位了很久的pom文件配置,下面的配置把.html资源加在include中即可(或者直接将includes删除也可以达到相同目的)


	
		src/main/resources
		
			***.properties
			**/*.xml
		
		false
	

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/851369.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号