SpringBoot首頁設(shè)置解析(推薦)
首先來解釋一下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;
}
}
當(dāng)找到對應(yīng)文件的時候就返回對應(yīng)的資源,這就是SpringBoot設(shè)置首頁的默認方式的原理。
從源碼中也可以看出另一個關(guān)于靜態(tài)資源目錄優(yōu)先級的問題。getWelcomePage遍歷靜態(tài)資源目錄,一旦找到就返回,所以優(yōu)先級和staticLocations中的順序相對,驗證一下。
先在每一個目錄下建立對應(yīng)的indx.html文件


和得出的結(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>

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>

擴展:優(yōu)先級問題
之前的三個方法都是單獨設(shè)置的,現(xiàn)在把他們結(jié)合起來


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

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

