Springboot-自定義錯誤頁面(4xx/5xx)
阿新 • • 發佈:2018-12-14
此方法適用於專案打jar包的方式!!!
1、在config包下建立ErrorConfiguration類:
import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; /** * <p><b>自定義錯誤頁面(4xx/5xx)處理類</b></p> * @Description 自定義錯誤頁面(4xx/5xx)處理類</ p> * @author MengMeng * Created date: 2018/10/11 * @Since JDK 1.80_171 */ @Component public class ErrorConfiguration implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { ErrorPage[] errorPages = new ErrorPage[]{ new ErrorPage(HttpStatus.FORBIDDEN, "/error/403"), new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"), new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"), new ErrorPage(Throwable.class, "/error/500") }; registry.addErrorPages(errorPages); } }
2、在controller包下建立ErrorController:
import org.apache.catalina.servlet4preview.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; @Controller public class ErrorController { /** *<p><b>自定義錯誤處理的Controller類</b></p> * @Description 自定義錯誤處理的Controller類</ p> * @author MengMeng *<p>Created date: 2018/10/11</p> * @version: 0.1 * @since JDK 1.80_144 */ @RequestMapping(value = "/error/{code}") public String error(@PathVariable int code, Model model) { String pager = ""; switch (code) { case 403: model.addAttribute("code", 403); pager = "error/page"; break; case 404: model.addAttribute("code", 404); pager = "error/page"; break; case 500: model.addAttribute("code", 500); pager = "error/page"; break; } return pager; } }
3、建立錯誤頁面,HTML程式碼:
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <!-- Title:錯誤處理頁 Description:錯誤處理頁 Author:MengMeng Created date: 2018/10/11 Version: Html5 --> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <title>網站訪問失敗</title> <link rel="shortcut icon" th:href="@{/images/logo-black.png}" href="/images/logo.png" type="images/x-icon" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="" /> <!-- 引入css --> <th:block th:replace="pageFrame/basicsCss :: copy" /> </head> <body class="hold-transition login-page"> <div class="login-box"> <!-- login-box-body --> <div id="doc_main"> <section class="bd clearfix"> <div class="module-error"> <div class="error-main clearfix"> <div class="info"> <h2 class="title">你所訪問的頁面不存在!</h2> <div class="reason"> <h3>可能的原因:</h3> <h3>1.內容填寫有錯誤。</h3> <h3>2.連結過了保質期。</h3> <h3>3.所在頁面時間過長。</h3> <h3>4.如果還未解決,請向管理員反饋!</h3> </div> <div class="oper"> <h3> <a href="/login">回到網站首頁></a> </h3> </div> </div> </div> </div> </section> </div> <!-- 引入js【body末尾引入js檔案會使頁面載入更快】 --> <th:block th:replace="pageFrame/basicsJs :: copy" /> <!-- bootstrapValidator --> <script th:src="@{/plugins/bootstrapValidator/js/bootstrapValidator.min.js}"></script> <script th:src="@{/plugins/bootstrapValidator/js/language/zh_CN.js}"></script> </body> </html>
效果圖:
相關學習連結: