SpringBoot首頁(yè)設(shè)置解析(推薦)
首先來(lái)解釋一下SpringBoot首頁(yè)設(shè)置的三種方式
1.SpringBoot默認(rèn)首頁(yè)設(shè)置
編寫一個(gè)最簡(jiǎn)單的html文件 index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>首頁(yè)</h1> </body> </html>
將index.html文件置于SpringBoot的任一靜態(tài)資源目錄下

http://localhost:8080/訪問(wèn),成功顯示

源碼分析
首先找對(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注冊(cè)了WelcomePageHandlerMappingBean來(lái)處理項(xiàng)目的默認(rèn)首頁(yè),構(gòu)造器中的this.getWelcomePage()為首頁(yè)資源。
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/"};
顯而易見(jiàn),這其實(shí)就是SpringBoot的靜態(tài)資源目錄
/META-INF
/resources/
/resources/
/static/
/public/
回到之前的代碼,獲取了StaticLocations后,通過(guò)循環(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è)置首頁(yè)的默認(rèn)方式的原理。
從源碼中也可以看出另一個(gè)關(guān)于靜態(tài)資源目錄優(yōu)先級(jí)的問(wèn)題。getWelcomePage遍歷靜態(tài)資源目錄,一旦找到就返回,所以優(yōu)先級(jí)和staticLocations中的順序相對(duì),驗(yàn)證一下。
先在每一個(gè)目錄下建立對(duì)應(yīng)的indx.html文件

http://localhost:8080/訪問(wèn)

和得出的結(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";
}
}
首頁(yè)資源indexController.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>indexController首頁(yè)</h1> </body> </html>
http://localhost:8080/訪問(wèn)

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");
}
}
首頁(yè)資源indexMVC.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>indexMVC首頁(yè)</h1> </body> </html>
http://localhost:8080/訪問(wèn)

擴(kuò)展:優(yōu)先級(jí)問(wèn)題
之前的三個(gè)方法都是單獨(dú)設(shè)置的,現(xiàn)在把他們結(jié)合起來(lái)

http://localhost:8080/訪問(wèn)

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

得出結(jié)論:Controller>MyMvcConfiguration>默認(rèn)方法
到此這篇關(guān)于SpringBoot首頁(yè)設(shè)置解析詳解的文章就介紹到這了,更多相關(guān)SpringBoot首頁(yè)設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解如何使用python創(chuàng)建和結(jié)束線程
線程的創(chuàng)建和結(jié)束是多線程編程中的核心概念之一,在本文中,我們將學(xué)習(xí)如何使用 Python 創(chuàng)建線程,并探討如何優(yōu)雅地結(jié)束線程,需要的朋友可以參考下2024-04-04
淺談Tensorflow2對(duì)GPU內(nèi)存的分配策略
本文主要介紹了Tensorflow2對(duì)GPU內(nèi)存的分配策略,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
python利用wx實(shí)現(xiàn)界面按鈕和按鈕監(jiān)聽(tīng)和字體改變的方法
今天小編就為大家分享一篇python利用wx實(shí)現(xiàn)界面按鈕和按鈕監(jiān)聽(tīng)和字體改變的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Pytorch深度學(xué)習(xí)gather一些使用問(wèn)題解決方案
這篇文章主要為大家介紹了Pytorch深度學(xué)習(xí),在使用gather過(guò)程中遇到的一下問(wèn)題,下面給出解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
Python argparse中的action=store_true用法小結(jié)
這篇文章主要介紹了Python argparse中的action=store_true用法小結(jié),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
Python進(jìn)階之如何快速將變量插入有序數(shù)組
在我們學(xué)習(xí)python的過(guò)程中,學(xué)習(xí)序列是一門必修課。本文我們就來(lái)一起看一看Python是如何快速將變量插入有序數(shù)組的,感興趣的可以了解一下2023-04-04
Python如何實(shí)現(xiàn)Paramiko的二次封裝
這篇文章主要介紹了Python如何實(shí)現(xiàn)Paramiko的二次封裝,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法
這篇文章主要介紹了python實(shí)現(xiàn)中文轉(zhuǎn)換url編碼的方法,結(jié)合實(shí)例形式分析了Python針對(duì)中文的gbk與utf-8編碼轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

