SpringBoot首页设置解析(推荐)


Posted in Python onFebruary 11, 2021

首先来解释一下SpringBoot首页设置的三种方式

1.SpringBoot默认首页设置

编写一个最简单的html文件 index.html

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
</head>
<body>
<h1>首页</h1>
</body>
</html>

将index.html文件置于SpringBoot的任一静态资源目录下

SpringBoot首页设置解析(推荐)

http://localhost:8080/访问,成功显示

SpringBoot首页设置解析(推荐)

源码分析

首先找对应的自动配置类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;
}

可以看到 SpringBoot注册了WelcomePageHandlerMappingBean来处理项目的默认首页,构造器中的this.getWelcomePage()为首页资源。

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

分析这段代码,首先获取了this.resourceProperties的StaticLocations字段,顾名思义就是静态路径,那就先跟踪StaticLocations

SpringBoot首页设置解析(推荐)

可以看出StaticLocations是WebPropertis中内部静态类Resources的属性,从构造器中可以看出它的值为

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

显而易见,这其实就是SpringBoot的静态资源目录

/META-INF

/resources/

/resources/

/static/

/public/

回到之前的代码,获取了StaticLocations后,通过循环遍历,很明显可以看到一个新的方法this.getIndexHtml(location)

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

使用this.resourceLoader返回一个与location对应的Resource执行另一个getIndexHtml()函数

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文件。再往回看

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

当找到对应文件的时候就返回对应的资源,这就是SpringBoot设置首页的默认方式的原理。

从源码中也可以看出另一个关于静态资源目录优先级的问题。getWelcomePage遍历静态资源目录,一旦找到就返回,所以优先级和staticLocations中的顺序相对,验证一下。

先在每一个目录下建立对应的indx.html文件

SpringBoot首页设置解析(推荐)

http://localhost:8080/访问

SpringBoot首页设置解析(推荐)

和得出的结论一样,优先级最高的是 /META-INF/resources/,把 /META-INF/resources/下的index.html文件删除再次验证

SpringBoot首页设置解析(推荐)

验证成功!

2.controller里添加"/"的映射路径

新建IndexController.java

package com.springboot04webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

 @RequestMapping("/")
 public String index(){
 return "indexController";
 }
}

首页资源indexController.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
</head>
<body>
<h1>indexController首页</h1>
</body>
</html>

http://localhost:8080/访问

SpringBoot首页设置解析(推荐)

3.MVC扩展配置实现

新建MyMvcConfiguration配置类,扩展MVC配置,重写addViewControllers方法

package com.springboot04webapp.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer {

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

首页资源indexMVC.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
</head>
<body>
<h1>indexMVC首页</h1>
</body>
</html>

http://localhost:8080/访问

SpringBoot首页设置解析(推荐)

扩展:优先级问题

之前的三个方法都是单独设置的,现在把他们结合起来

SpringBoot首页设置解析(推荐)

http://localhost:8080/访问

SpringBoot首页设置解析(推荐)

优先级最高的是第二种方法,然后将indexController删除,再次验证

SpringBoot首页设置解析(推荐)

得出结论:Controller>MyMvcConfiguration>默认方法

到此这篇关于SpringBoot首页设置解析详解的文章就介绍到这了,更多相关SpringBoot首页设置内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python中的类与对象之描述符详解
Mar 27 Python
python实现在sqlite动态创建表的方法
May 08 Python
Python实现统计代码行的方法分析
Jul 12 Python
python中ImageTk.PhotoImage()不显示图片却不报错问题解决
Dec 06 Python
用Python中的turtle模块画图两只小羊方法
Apr 09 Python
python批量修改ssh密码的实现
Aug 08 Python
如何使用Python抓取网页tag操作
Feb 14 Python
通过python连接Linux命令行代码实例
Feb 18 Python
python2 对excel表格操作完整示例
Feb 23 Python
在Pytorch中使用Mask R-CNN进行实例分割操作
Jun 24 Python
jupyter notebook远程访问不了的问题解决方法
Jan 11 Python
Python制作动态字符画的源码
Aug 04 Python
使用Python爬取小姐姐图片(beautifulsoup法)
Feb 11 #Python
详解python日志输出使用配置文件格式
Feb 10 #Python
python 获取域名到期时间的方法步骤
Feb 10 #Python
Numpy ndarray 多维数组对象的使用
Feb 10 #Python
Python将QQ聊天记录生成词云的示例代码
Feb 10 #Python
python利用文件时间批量重命名照片和视频
Feb 09 #Python
python opencv实现图像配准与比较
Feb 09 #Python
You might like
php守护进程 加linux命令nohup实现任务每秒执行一次
2011/07/04 PHP
JoshChen_web格式编码UTF8-无BOM的小细节分析
2013/08/16 PHP
UTF-8正则表达式如何匹配汉字
2015/08/03 PHP
CI框架AR操作(数组形式)实现插入多条sql数据的方法
2016/05/18 PHP
php实现xml转换数组的方法示例
2017/02/03 PHP
Docker搭建自己的PHP开发环境
2018/02/24 PHP
PHP结合Redis+MySQL实现冷热数据交换应用案例详解
2019/07/09 PHP
[原创]后缀就扩展名为js的文件是什么文件
2007/12/06 Javascript
学习ExtJS table布局
2009/10/08 Javascript
javascript两段代码,两个小技巧
2010/02/04 Javascript
JavaSript中变量的作用域闭包的深入理解
2014/05/12 Javascript
JS+CSS实现仿雅虎另类滑动门切换效果
2015/10/13 Javascript
javascript字符串替换函数如何一次性全部替换掉
2015/10/30 Javascript
使用AJAX实现Web页面进度条的实例分享
2016/05/06 Javascript
一道优雅面试题分析js中fn()和return fn()的区别
2016/07/05 Javascript
Bootstrap基本插件学习笔记之模态对话框(16)
2016/12/08 Javascript
angular ng-repeat数组中的数组实例
2017/02/18 Javascript
vue.js 初体验之Chrome 插件开发实录
2017/05/13 Javascript
webpack本地开发环境无法用IP访问的解决方法
2018/03/20 Javascript
解决Vue中mounted钩子函数获取节点高度出错问题
2018/05/18 Javascript
Linux系统(CentOS)下python2.7.10安装
2018/09/26 Python
Python for循环与range函数的使用详解
2019/03/23 Python
python变量命名的7条建议
2019/07/04 Python
Python 分享10个PyCharm技巧
2019/07/13 Python
python爬取百度贴吧前1000页内容(requests库面向对象思想实现)
2019/08/10 Python
Python装饰器使用你可能不知道的几种姿势
2019/10/25 Python
python 协程中的迭代器,生成器原理及应用实例详解
2019/10/28 Python
python 实现 hive中类似 lateral view explode的功能示例
2020/05/18 Python
Python filter过滤器原理及实例应用
2020/08/18 Python
HTML5所有标签汇总及标签意义解释
2015/03/12 HTML / CSS
墨尔本照明批发商店:Mica Lighting
2017/12/28 全球购物
英国领先的在线旅游和休闲零售商:lastminute.com
2019/01/23 全球购物
MyBag中文网:英国著名的时尚包袋电商零售网站
2020/07/31 全球购物
银行主办会计岗位职责
2014/08/13 职场文书
第二批党的群众路线教育实践活动个人整改方案
2014/10/31 职场文书
人工作失职检讨书
2015/05/05 职场文书