SpringBoot設置默認主頁的方法步驟
1.若采用渲染引擎,JSP等VIEW渲染技術,可以通過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類中并未找到相關方法。也無其他解決方案。
即
@Controller
@RequestMapping("/")
public class IndexController {
@RequestMapping("/")
public void index(HttpServletResponse response) throws IOException {
response.sendRedirect("/temples/index.html");
}
}
3最后 最好通過nginx配置 不要在后臺項目代碼里添加前端的文件。
到此這篇關于SpringBoot設置默認主頁的方法步驟的文章就介紹到這了,更多相關SpringBoot設置默認主頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java NIO Path接口和Files類配合操作文件的實例
下面小編就為大家分享一篇Java NIO Path接口和Files類配合操作文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11
基于Spring Boot不同的環(huán)境使用不同的配置方法
下面小編就為大家分享一篇基于Spring Boot不同的環(huán)境使用不同的配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
SpringBoot 過濾器, 攔截器, 監(jiān)聽器的具體使用
本文主要介紹了SpringBoot 過濾器, 攔截器, 監(jiān)聽器的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05
Fluent MyBatis實現(xiàn)動態(tài)SQL
MyBatis 令人喜歡的一大特性就是動態(tài) SQL。本文主要介紹了Fluent MyBatis實現(xiàn)動態(tài)SQL,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Springboot2.x 使用 Log4j2 異步打印日志的實現(xiàn)
這篇文章主要介紹了Springboot2.x 使用 Log4j2 異步打印日志的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12

