SpringBoot 指定首頁和404頁面

一、定製首頁:
方式一:SpringBoot自動映射
在靜態資源目錄resources、static、public的其中一個目錄中創建index.html文件,springBoot會自動識別,將這個文件作爲首頁訪問

在这里插入图片描述

方式二:使用thymeleaf模板引擎
1.導入依賴

<!--Thymeleaf模板引擎依賴-->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

2.resources目錄下的templates目錄中創建index.html(文件名稱不強制,只要與控制層對應就行)

//java項目www.fhadmin.org
@Controller
public class PageController {
    @RequestMapping("/")
    public String index(Model model){
        model.addAttribute("msg","首頁");
        return "index"; //thymeleaf引擎幫我們配置好了視圖解析器,實際返回的頁面爲templates目錄下的index.html
    }
}

二、定製404頁面:

在導入了Thymeleaf模板引擎依賴的前提下,定製404頁面非常簡單,在templates目錄下創建error目錄,然後error目錄中創建404.html,Thymeleaf會自動將這個頁面定製爲404頁面
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章