佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > SpringBoot首頁設(shè)置解析(推薦)

SpringBoot首頁設(shè)置解析(推薦)

熱門標(biāo)簽:呼叫中心市場需求 硅谷的囚徒呼叫中心 客戶服務(wù) 百度AI接口 企業(yè)做大做強(qiáng) 語音系統(tǒng) Win7旗艦版 電話運(yùn)營中心

首先來解釋一下SpringBoot首頁設(shè)置的三種方式

1.SpringBoot默認(rèn)首頁設(shè)置

編寫一個(gè)最簡單的html文件 index.html

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

將index.html文件置于SpringBoot的任一靜態(tài)資源目錄下

http://localhost:8080/訪問,成功顯示

源碼分析

首先找對(duì)應(yīng)的自動(dòng)配置類WebMvcAutoConfiguration中的對(duì)應(yīng)代碼

@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來處理項(xiàng)目的默認(rèn)首頁,構(gòu)造器中的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字段,顧名思義就是靜態(tài)路徑,那就先跟蹤StaticLocations

可以看出StaticLocations是WebPropertis中內(nèi)部靜態(tài)類Resources的屬性,從構(gòu)造器中可以看出它的值為

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

顯而易見,這其實(shí)就是SpringBoot的靜態(tài)資源目錄

/META-INF

/resources/

/resources/

/static/

/public/

回到之前的代碼,獲取了StaticLocations后,通過循環(huán)遍歷,很明顯可以看到一個(gè)新的方法this.getIndexHtml(location)

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

使用this.resourceLoader返回一個(gè)與location對(duì)應(yīng)的Resource執(zhí)行另一個(gè)getIndexHtml()函數(shù)

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

很明顯,這個(gè)方法是獲取對(duì)應(yīng)目錄下的index.html文件。再往回看

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

當(dāng)找到對(duì)應(yīng)文件的時(shí)候就返回對(duì)應(yīng)的資源,這就是SpringBoot設(shè)置首頁的默認(rèn)方式的原理。

從源碼中也可以看出另一個(gè)關(guān)于靜態(tài)資源目錄優(yōu)先級(jí)的問題。getWelcomePage遍歷靜態(tài)資源目錄,一旦找到就返回,所以優(yōu)先級(jí)和staticLocations中的順序相對(duì),驗(yàn)證一下。

先在每一個(gè)目錄下建立對(duì)應(yīng)的indx.html文件

http://localhost:8080/訪問

和得出的結(jié)論一樣,優(yōu)先級(jí)最高的是 /META-INF/resources/,把 /META-INF/resources/下的index.html文件刪除再次驗(yàn)證

驗(yàn)證成功!

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擴(kuò)展配置實(shí)現(xiàn)

新建MyMvcConfiguration配置類,擴(kuò)展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/訪問

擴(kuò)展:優(yōu)先級(jí)問題

之前的三個(gè)方法都是單獨(dú)設(shè)置的,現(xiàn)在把他們結(jié)合起來

http://localhost:8080/訪問

優(yōu)先級(jí)最高的是第二種方法,然后將indexController刪除,再次驗(yàn)證

得出結(jié)論:Controller>MyMvcConfiguration>默認(rèn)方法

到此這篇關(guān)于SpringBoot首頁設(shè)置解析詳解的文章就介紹到這了,更多相關(guān)SpringBoot首頁設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • SpringBoot設(shè)置首頁(默認(rèn)頁)跳轉(zhuǎn)功能的實(shí)現(xiàn)方案
  • SpringBoot http post請(qǐng)求數(shù)據(jù)大小設(shè)置操作
  • springboot+idea熱啟動(dòng)設(shè)置方法(自動(dòng)加載)
  • SpringBoot設(shè)置接口超時(shí)時(shí)間的方法

標(biāo)簽:崇左 喀什 濟(jì)南 山西 長沙 山西 海南 安康

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SpringBoot首頁設(shè)置解析(推薦)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    三都| 平顶山市| 石景山区| 望江县| 吉安市| 洛川县| 淳化县| 邯郸市| 桂平市| 临沭县| 大洼县| 曲水县| 东台市| 正宁县| 石台县| 榕江县| 榆树市| 得荣县| 东阳市| 五华县| 麦盖提县| 大名县| 宜城市| 镇坪县| 华容县| 成都市| 阿巴嘎旗| 泉州市| 黄石市| 叙永县| 惠州市| 遂溪县| 台南县| 德昌县| 南溪县| 武平县| 曲靖市| 盐边县| 金湖县| 广东省| 彝良县|