SpringBoot首页设置解析(推荐)
作者:zyxzcr 发布时间:2021-11-03 05:43:00
首先来解释一下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的任一静态资源目录下
http://localhost:8080/访问,成功显示
源码分析
首先找对应的自动配置类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
可以看出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文件
http://localhost:8080/访问
和得出的结论一样,优先级最高的是 /META-INF/resources/,把 /META-INF/resources/下的index.html文件删除再次验证
验证成功!
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/访问
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/访问
扩展:优先级问题
之前的三个方法都是单独设置的,现在把他们结合起来
http://localhost:8080/访问
优先级最高的是第二种方法,然后将indexController删除,再次验证
得出结论:Controller>MyMvcConfiguration>默认方法
来源:https://blog.csdn.net/zcrzcrzcrzcrzcr/article/details/113784517


猜你喜欢
- 大概来介绍一下 Django Allauth 改造的期间遇到的一些问题和改造方法,在此之前我只想说——Django Allauth 是屑。为
- 目录关于Web服务器和应用服务器Python项目部署架构关于cgi、wsgi、uwsgi、http协议关于cgi、fastcgi、php-f
- 前言之前工作中主要使用的是 Tensorflow 1.15 版本,但是渐渐跟不上工作中的项目需求了,而且因为 2.x 版本和 1.x 版本差
- python是一个很有趣的语言,可以在命令行窗口运行。python中有很多功能强大的模块,这篇经验告诉你,如何使用python的pygal模
- 一、前言春节即将来临,大家肯定各种掏腰包花花花,小编相信大家在支付时候,微信、支付宝支付肯定是优先选择。今天小编心血来潮,为大家带来一个很有
- '-----------------------------------------------------------
- 背景堆是一种非常常用的数据结构,它能够支持在O(1)的时间复杂度获取到最大值(或最小值),因此我们经常在需要求最值的场景使用它。然而普通堆它
- 引言在Go语言中,我们通常会用到panic和recover来抛出错误和捕获错误,这一对操作在单协程环境下我们正常用就好了,并不会踩到什么坑。
- numpy.where (condition[, x, y])numpy.where() 有两种用法:1. np.where(conditi
- 一.简单介绍: functools模块用于高阶函数:作用于或返回其他函数的函数。一般而言,任何可调用对象都可以作为本模块用途的函数
- OR、in和union all 查询效率到底哪个快?网上很多的声音都是说union all 快于 or、in,因为or、in会导致全表扫描,
- 对于熟悉 C/C++ 或 Java 语言的工程师来说,JavaScript 显得灵活,简单易懂,对代码的格式的要求也相对松散。很容易学习,并
- 1、冒泡排序法让列表中的一项和下一项作比较,若前一项大于后一项则交换两者位置(升序)。方法一:直接使用for循环L=[8,2,50,3]fo
- 概念json是一种通用的数据类型一般情况下接口返回的数据类型都是json长得像字典,形式也是k-v{ }其实json是字符串字符串不能用ke
- 介绍毫无疑问,任何一个试图使用 CSS 的网页设计师和开发人员都会发现不同的浏览器要求不同的样式声明。这些烦恼归咎于各浏览器及其各版本不同程
- theme: channing-cyan网页伪静态将 * 页伪装成静态网页,可以提升网页被搜索引擎检索道德概率表现形式为:网址看着像是一个具
- 本文实例为大家分享了原生js实现波浪导航效果的具体代码,供大家参考,具体内容如下展示效果:源码展示:<!doctype html>
- 随着网页制作热潮的兴起,Dreamweaver 4.0强大的功能深受众多网页制作者的喜爱。特别是Dreamweaver 4.0中有许多第三方
- 透视表是一种可以对数据动态排布并且分类汇总的表格格式。对于熟练使用 excel 的伙伴来说,一定很是亲切!pd.pivot_table()
- python os.stat()获取相关文件的系统状态信息stat 系统调用时用来返回相关文件的系统状态信息的。下面直接以一个具体示例来进行