springboot 自定義404、500錯誤提示頁面的實(shí)現(xiàn)
springboot 默認(rèn)的異常處理機(jī)制
springboot 默認(rèn)已經(jīng)提供了一套處理異常的機(jī)制。一旦程序中出現(xiàn)了異常 springboot 會向 /error 的 url 發(fā)送請求。在 springboot 中提供了一個名為 BasicErrorController 的類來處理 /error 請求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁面來展示異常信息
使用模板引擎
在使用 thymeleaf 等模板引擎時,springboot 會自動到 src/main/resources/templates/error/,文件夾下尋找 404.html、500.html 的錯誤提示頁面
錯誤提示頁面的命名規(guī)則就是:錯誤碼.html,如 404 是 404.html,500 是 500.html
使用示例
創(chuàng)建 springboot 項(xiàng)目如下
404、500 錯誤提示頁面結(jié)構(gòu)如下

application.properties 項(xiàng)目配置文件
server.port=8080 #它的默認(rèn)值就是classpath:/templates/,源碼在ThymeleafProperties類中 spring.mvc.view.prefix=classpath:/templates/ #它的默認(rèn)值就是.html,源碼在ThymeleafProperties類中 spring.mvc.view.suffix=.html spring.thymeleaf.cache=false
404 頁面內(nèi)容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>404</title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" rel="external nofollow" />
<link rel="stylesheet" type="text/css" th:href="@{/css/404.css}" rel="external nofollow" />
</head>
<body>
<div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>
500 頁面內(nèi)容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>500</title>
<link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow" rel="external nofollow" />
<link rel="stylesheet" type="text/css" th:href="@{/css/500.css}" rel="external nofollow" />
</head>
<body>
<div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>
controller 如下
@Controller
public class PageController {
// 跳轉(zhuǎn)到登錄頁
@GetMapping(path = "/toLogin")
public String toLogin() {
int code = 1/0;
return "login";
}
}
404.html 頁面測試
訪問不存在的接口:http://localhost:8080/aaaa,結(jié)果如下

500.html 頁面測試
訪問已存在的接口:http://localhost:8080/toLogin,結(jié)果如下

沒有使用模板引擎
如果沒有使用 thymeleaf 等模板引擎時,springboot 會到靜態(tài)資源文件夾尋找 404.htm、500.html的錯誤提示頁面,命名同上。springboot 中默認(rèn)的靜態(tài)資源路徑有 4 個,分別是
classpath:/METAINF/resources/classpath:/resources/classpath:/static/classpath:/public/
優(yōu)先級順序?yàn)椋?code>META-INF/resources > resources > static > public,以上 4 種路徑創(chuàng)建 error 文件夾,再創(chuàng)建 404、500 錯誤提示頁面如下

不用寫額外的映射器,就能直接請求到
到此這篇關(guān)于springboot 自定義404、500錯誤提示頁面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot 自定義錯誤頁面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Java?float和double精度范圍大小
這篇文章主要介紹了關(guān)于Java?float和double精度范圍大小,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringApplicationRunListener監(jiān)聽器源碼詳解
這篇文章主要介紹了SpringApplicationRunListener監(jiān)聽器源碼詳解,springboot提供了兩個類SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架還提供了一個ApplicationListener接口,需要的朋友可以參考下2023-11-11
springboot下添加日志模塊和設(shè)置日志文件輸出的方法
日志的使用將通過SLF4J來使用,SLF4J是一個為Java應(yīng)用提供簡單日志記錄的接口,在Spring框架中,SLF4J常常用于處理框架本身以及應(yīng)用程序的日志記錄,本文給大家介紹springboot下添加日志模塊和設(shè)置日志文件輸出的相關(guān)知識,感興趣的朋友一起看看吧2023-12-12
java實(shí)現(xiàn)連連看游戲課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)連連看游戲課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

