SpringBoot首頁設(shè)置解析(推薦)
首先來解釋一下SpringBoot首頁設(shè)置的三種方式
1.SpringBoot默認(rèn)首頁設(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來處理項目的默認(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/"};
顯而易見,這其實就是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è)置首頁的默認(rèn)方式的原理。
從源碼中也可以看出另一個關(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擴(kuò)展配置實現(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>
擴(kuò)展:優(yōu)先級問題
之前的三個方法都是單獨設(shè)置的,現(xiàn)在把他們結(jié)合起來
優(yōu)先級最高的是第二種方法,然后將indexController刪除,再次驗證
得出結(jié)論:Controller>MyMvcConfiguration>默認(rèn)方法
到此這篇關(guān)于SpringBoot首頁設(shè)置解析詳解的文章就介紹到這了,更多相關(guān)SpringBoot首頁設(shè)置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)簡易的學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡易的學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05python常用時間庫time、datetime與時間格式之間的轉(zhuǎn)換教程
Python項目中很多時候會需要將時間在Datetime格式和TimeStamp格式之間轉(zhuǎn)化,下面這篇文章主要給大家介紹了關(guān)于python常用時間庫time、datetime與時間格式之間轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2023-02-02如何利用python實現(xiàn)windows的批處理及文件夾操作
最近工作中需要幾個腳本運(yùn)行其他程序,幾乎像一個Windows批處理文件,這篇文章主要給大家介紹了關(guān)于如何利用python實現(xiàn)windows的批處理及文件夾操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01pandas創(chuàng)建DataFrame的7種方法小結(jié)
這篇文章主要介紹了pandas創(chuàng)建DataFrame的7種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06