SpringBoot首页设置解析(推荐)

首先来解释一下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>默认方法

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

(0)

相关推荐

  • SpringBoot http post请求数据大小设置操作

    背景: 使用http post请求方式的接口,使用request.getParameter("XXX");的方法获取参数的值,当数据量超过几百k的时候,接口接收不到数据或者接收为null. @RequestMapping(value = "/rcv",method = RequestMethod.POST) public ResInfo<String> pullApi(HttpServletRequest request) { String channe

  • springboot+idea热启动设置方法(自动加载)

    springboot+idea热启动 1.在pom.xml中引入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> 然后在build/plugins/plugin中设置

  • SpringBoot设置首页(默认页)跳转功能的实现方案

    先给大家介绍下SpringBoot设置首页(默认页)跳转功能 最近springboot开发需要设置个默认页面,就相当于我访问http://www.back.order.baidu.com要直接跳转到登录页面. 方案1:controller里添加一个"/"的映射路径 @RequestMapping("/") public String index(Model model, HttpServletResponse response) { model.addAttribu

  • SpringBoot设置接口超时时间的方法

    SpringBoot设置接口访问超时时间有两种方式 一.在配置文件application.properties中加了spring.mvc.async.request-timeout=20000,意思是设置超时时间为20000ms即20s, 二.还有一种就是在config配置类中加入: public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureAsyncSupport(fin

  • SpringBoot首页设置解析(推荐)

    首先来解释一下SpringBoot首页设置的三种方式 1.SpringBoot默认首页设置 编写一个最简单的html文件 index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>首页</h1> </body> </html&g

  • SpringBoot 静态资源导入及首页设置问题

    本节了解一下 SpringBoot 中 Web 开发的静态资源导入和首页设置,对应 SpringBoot-03-Web 项目. 1. 静态资源导入 在 Web 开发过程中,我们需要接触许多的静态资源,如 CSS.JS.图片等:在之前的开发过程中,这些资源都放在 Web 的目录下,用到的时候按照对应路径访问即可.不过在 SpringBoot 项目中,没有了 Web 的目录,那这些静态资源该放到哪里去,又要如何访问呢? 由于是 Web 应用中的配置,所以查看对应的自动配置类 WebMvcAutoCo

  • SpringBoot随机数设置及参数间引用的操作步骤

    自定义配置 SpringBoot免除了项目中大部分手动配置,可以说,几乎所有的配置都可以写在全局配置文件application.peroperties中,SpringBoot会自动加载全局配置文件从而免除我们手动加载的烦恼.但是,如果我们自定义了配置文件,那么SpringBoot是无法识别这些配置文件的,此时需要我们手动加载. 接下来,将针对SpringBoot的自定义配置文件及其加载方式进行讲解. (1)使用@PropertySource加载配置文件 我们可以使用@PropertySource

  • python matplotlib中文显示参数设置解析

    最近在学习python著名的绘图包matplotlib时发现,有时候图例等设置无法正常显示中文,于是就想把这个问题解决了. PS:本文仅针对Windows,其他平台仅供参考. 原因 大致就是matplotlib库中没有中文字体. 我安装的anaconda,这是对应的matplotlib的字体所在文件夹(怎么找到matplotlib配置文件夹所在,下面解决方案会叙述,easyman~). C:\Anaconda64\Lib\site-packages\matplotlib\mpl-data\fon

  • SpringCloud Netflix Ribbon源码解析(推荐)

    SpringCloud Netflix Ribbon源码解析 首先会介绍Ribbon 相关的配置和实例的初始化过程,然后讲解Ribbon 是如何与OpenFeign 集成的,接着讲解负载均衡器LoadBalancerCli ent , 最后依次讲解ILoadB alancer的实现和负载均衡策略Rule 的实现. 配置和实例初始化 @RibbonClient 注解可以声明Ribbon 客户端,设置Ribbon 客户端的名称和配置类,configuration 属性可以指定@Configurati

  • 采用React编写小程序的Remax框架的编译流程解析(推荐)

    Remax是蚂蚁开源的一个用React来开发小程序的框架,采用运行时无语法限制的方案.整体研究下来主要分为三大部分:运行时原理.模板渲染原理.编译流程:看了下现有大部分文章主要集中在Reamx的运行时和模板渲染原理上,而对整个React代码编译为小程序的流程介绍目前还没有看到,本文即是来补充这个空白. 关于模板渲染原理看这篇文章:https://www.jb51.net/article/132635.htm 关于remax运行时原理看这篇文章:https://www.jb51.net/artic

  • SpringBoot实现设置全局和局部时间格式化

    目录 前言 解决问题 1 全局设置(推荐) 2 局部设置(不推荐) 总结 前言 在开发中,我们进行数据库查询日期类型的时候,往往不是我们想要的时间格式,比如下面的情况: 我数据库里面把这些日期相关的字段都设置为datatime格式了: 数据类型如下: 实体是这么配置的: 那么默认查询出来的结果就是这样的: 救命啊,这什么格式啊,完全不是我想要的年月日时分秒. 其实想解决这种问题也很简单,无非就是时间格式不对吗,下面通过全局设置和局部设置两个方面解决这种问题 解决问题 1 全局设置(推荐) 一般来

  • springboot实现FastJson解析json数据的方法

    最近在研究springboot实现FastJson解析json数据的方法,那么今天也算个学习笔记吧! 添加jar包: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> 两种方式启动加载类: 第一种继承WebMvcConfigur

  • jQuery表单验证插件解析(推荐)

    一:插件 (1) Validform_v5.3.1_min.js Validform_Datatype.js (2)网址:http://validform.rjboy.cn 在这个网站上有demo和插件下载链接. 二:普通验证 (1)Validform_Datatype.js /* Validform datatype extension By sean during December 8, 2012 - February 20, 2013 For more information, pleas

  • jQuery动态增减行的实例代码解析(推荐)

    先给大家展示下效果图: 这是没有增加时的界面: 增加后的界面: 删除后的界面: 原因分析: 不擅长jquery和JavaScript 场景: 代码如下: <table class="table table-bordered"> <thead> <tr> <th style="width: 10px">輪次</th> <th style="width: 100%">比賽時間&l

随机推荐