SpringBoot設(shè)置默認主頁的方法步驟
1.若采用渲染引擎,JSP等VIEW渲染技術(shù),可以通過addViewController的方式解決。
即:
@Configuration
public class DefaultView extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/Blog").setViewName("forward:index.jsp");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}
或者
@Controller
@RequestMapping("/")
public class IndexController {
@RequestMapping("/Blog")
public String index() {
return "forward:index.html";
}
}
2.若完全采用前后端分離的模式,即前端所有資源都放在addresourceHandler配置的路徑下
即
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/temples/**")
.addResourceLocations("classpath:/temples/");
super.addResourceHandlers(registry);
}
此時不能通過配置addViewController的方式解決,會拋出異常
即
javax.servlet.ServletException: Could not resolve view with name 'forward:/temples/index.html' in servlet with name 'dispatcherServlet'
只能通過response.redirect(“temples/index.html”)的方式重指向默認主頁,
注:我在WebMvcConfigurationSupport類中并未找到相關(guān)方法。也無其他解決方案。
即
@Controller
@RequestMapping("/")
public class IndexController {
@RequestMapping("/")
public void index(HttpServletResponse response) throws IOException {
response.sendRedirect("/temples/index.html");
}
}
3最后 最好通過nginx配置 不要在后臺項目代碼里添加前端的文件。
到此這篇關(guān)于SpringBoot設(shè)置默認主頁的方法步驟的文章就介紹到這了,更多相關(guān)SpringBoot設(shè)置默認主頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用java讀取web項目中json文件為map集合方法示例
這篇文章主要給大家介紹了關(guān)于利用java讀取web項目中json文件為map集合的相關(guān)資料,文中通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面來一起看看吧。2017-08-08
優(yōu)化Java內(nèi)存管理來防止“GC”錯誤的方法詳解
垃圾回收(GC)是 Java 中的一個重要機制,它可以管理內(nèi)存并回收不再使用的對象所占用的資源,在本文中,我們將探討一些技巧,幫助您避免這一錯誤,確保您的 Java 應(yīng)用程序順利運行,需要的朋友可以參考下2023-11-11
詳解SpringBoot基礎(chǔ)之banner玩法解析
SpringBoot項目啟動時會在控制臺打印一個默認的啟動圖案,這個圖案就是我們要講的banner,這篇文章主要介紹了SpringBoot基礎(chǔ)之banner玩法解析,感興趣的小伙伴們可以參考一下2019-04-04
詳解SpringBoot靜態(tài)方法獲取bean的三種方式
本文主要介紹了詳解SpringBoot靜態(tài)方法獲取bean的三種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10

