springboot 自定義404、500錯(cuò)誤提示頁(yè)面的實(shí)現(xiàn)
springboot 默認(rèn)的異常處理機(jī)制
springboot
默認(rèn)已經(jīng)提供了一套處理異常的機(jī)制。一旦程序中出現(xiàn)了異常 springboot
會(huì)向 /error
的 url
發(fā)送請(qǐng)求。在 springboot
中提供了一個(gè)名為 BasicErrorController
的類來(lái)處理 /error
請(qǐng)求,然后跳轉(zhuǎn)到默認(rèn)顯示異常的頁(yè)面來(lái)展示異常信息
使用模板引擎
在使用 thymeleaf
等模板引擎時(shí),springboot
會(huì)自動(dòng)到 src/main/resources/templates/error/
,文件夾下尋找 404.html、500.html
的錯(cuò)誤提示頁(yè)面
錯(cuò)誤提示頁(yè)面的命名規(guī)則就是:錯(cuò)誤碼.html
,如 404
是 404.html
,500
是 500.html
使用示例
創(chuàng)建 springboot
項(xiàng)目如下
404、500
錯(cuò)誤提示頁(yè)面結(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
頁(yè)面內(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
頁(yè)面內(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)到登錄頁(yè) @GetMapping(path = "/toLogin") public String toLogin() { int code = 1/0; return "login"; } }
404.html
頁(yè)面測(cè)試
訪問(wèn)不存在的接口:http://localhost:8080/aaaa
,結(jié)果如下
500.html
頁(yè)面測(cè)試
訪問(wèn)已存在的接口:http://localhost:8080/toLogin
,結(jié)果如下
沒(méi)有使用模板引擎
如果沒(méi)有使用 thymeleaf
等模板引擎時(shí),springboot
會(huì)到靜態(tài)資源文件夾尋找 404.htm、500.html
的錯(cuò)誤提示頁(yè)面,命名同上。springboot
中默認(rèn)的靜態(tài)資源路徑有 4
個(gè),分別是
classpath:/METAINF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
優(yōu)先級(jí)順序?yàn)椋?code>META-INF/resources > resources > static > public,以上 4
種路徑創(chuàng)建 error
文件夾,再創(chuàng)建 404、500
錯(cuò)誤提示頁(yè)面如下
不用寫額外的映射器,就能直接請(qǐng)求到
到此這篇關(guān)于springboot 自定義404、500錯(cuò)誤提示頁(yè)面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot 自定義錯(cuò)誤頁(yè)面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Java?float和double精度范圍大小
這篇文章主要介紹了關(guān)于Java?float和double精度范圍大小,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringApplicationRunListener監(jiān)聽(tīng)器源碼詳解
這篇文章主要介紹了SpringApplicationRunListener監(jiān)聽(tīng)器源碼詳解,springboot提供了兩個(gè)類SpringApplicationRunListeners、SpringApplicationRunListener(EventPublishingRunListener),spring框架還提供了一個(gè)ApplicationListener接口,需要的朋友可以參考下2023-11-11對(duì)象轉(zhuǎn)Json字符串時(shí)如何忽略指定屬性
這篇文章主要介紹了對(duì)象轉(zhuǎn)Json字符串時(shí)如何忽略指定屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08springboot下添加日志模塊和設(shè)置日志文件輸出的方法
日志的使用將通過(guò)SLF4J來(lái)使用,SLF4J是一個(gè)為Java應(yīng)用提供簡(jiǎn)單日志記錄的接口,在Spring框架中,SLF4J常常用于處理框架本身以及應(yīng)用程序的日志記錄,本文給大家介紹springboot下添加日志模塊和設(shè)置日志文件輸出的相關(guān)知識(shí),感興趣的朋友一起看看吧2023-12-12java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05