濮阳杆衣贸易有限公司

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

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

熱門標簽:高德地圖標注字母 千呼ai電話機器人免費 騰訊地圖標注有什么版本 鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 400電話辦理費用收費 柳州正規(guī)電銷機器人收費 申請辦個400電話號碼 外呼系統(tǒng)前面有錄音播放嗎 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商

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

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

編寫一個最簡單的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/訪問,成功顯示

源碼分析

首先找對應(yīng)的自動配置類WebMvcAutoConfiguration中的對應(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來處理項目的默認首頁,構(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/"};

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

/META-INF

/resources/

/resources/

/static/

/public/

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

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

使用this.resourceLoader返回一個與location對應(yīng)的Resource執(zhí)行另一個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;
}

很明顯,這個方法是獲取對應(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;
 }
}

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

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

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

http://localhost:8080/訪問

和得出的結(jié)論一樣,優(yōu)先級最高的是 /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擴展配置實現(xiàn)

新建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/訪問

擴展:優(yōu)先級問題

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

http://localhost:8080/訪問

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

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

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

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

標簽:哈爾濱 海南 大慶 平頂山 烏蘭察布 合肥 烏蘭察布 郴州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SpringBoot首頁設(shè)置解析(推薦)》,本文關(guān)鍵詞  SpringBoot,首頁,設(shè)置,解析,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SpringBoot首頁設(shè)置解析(推薦)》相關(guān)的同類信息!
  • 本頁收集關(guān)于SpringBoot首頁設(shè)置解析(推薦)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    锡林郭勒盟| 宁明县| 永和县| 惠州市| 石台县| 西充县| 平乐县| 弋阳县| 安龙县| 文登市| 阳西县| 隆子县| 太保市| 得荣县| 宁武县| 自治县| 抚远县| 城口县| 依兰县| 大悟县| 罗源县| 蓬溪县| 蓬莱市| 乐安县| 肃北| 钟祥市| 通江县| 托克逊县| 静乐县| 新野县| 陇南市| 库车县| 临沂市| 建德市| 邵阳县| 盐山县| 无极县| 茶陵县| 鄂伦春自治旗| 阿拉尔市| 嘉善县|