SpringBoot?錯誤頁面跳轉(zhuǎn)方式
SpringBoot錯誤頁面跳轉(zhuǎn)
SpringBoot實現(xiàn)MVC 404、500等錯誤時跳轉(zhuǎn)自定義頁面
一、新增配置類
package com.study.demo.config;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
/**
?* 錯誤頁面的配置
?*/
@Component
public class ErrorPageConfig implements ErrorPageRegistrar {
? ? @Override
? ? public void registerErrorPages(ErrorPageRegistry registry) {
? ? ? ? ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/errorPageController/error_400");
? ? ? ? ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/errorPageController/error_401");
? ? ? ? ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/errorPageController/error_404");
? ? ? ? ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/errorPageController/error_500");
? ? ? ? registry.addErrorPages(error400Page,error401Page,error404Page,error500Page);
? ? }
}二、錯誤頁面跳轉(zhuǎn)控制器
package com.study.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/errorPageController")
public class ErrorPageController {
? ? @RequestMapping("/error_{errorCode}")
? ? public String error(@PathVariable int errorCode){
? ? ? ? String responseMsg;
? ? ? ? switch (errorCode) {
? ? ? ? ? ? case 400: responseMsg = "/400.html"; break;
? ? ? ? ? ? case 401: responseMsg = "/401.html"; break;
? ? ? ? ? ? case 404: responseMsg = "/404.html"; break;
? ? ? ? ? ? case 500: responseMsg = "/500.html"; break;
? ? ? ? ? ? default: responseMsg = "/404.html"; break;
? ? ? ? }
? ? ? ? return responseMsg;
? ? }
}SpringBoot自定義錯誤頁面
一、錯誤頁面
請求出現(xiàn)錯誤時,跳轉(zhuǎn)到自定義的頁面中,比如404,假如沒對錯誤進(jìn)行處理,那么系統(tǒng)默認(rèn)的頁面與項目的頁面會有很大的不搭。
解決:在默認(rèn)的靜態(tài)路徑下,新建error文件,里面放入錯誤頁面,頁面命名為錯誤狀態(tài)碼,如:404.html,也可以命名為4xx.html,但如果兩個文件同時存在,那么會優(yōu)先展示404.html
注:靜態(tài)路徑為
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
?"classpath:/resources/",?
?"classpath:/static/",?
?"classpath:/public/"
?};
?// 注:還有一個默認(rèn)的根路徑 ? ?"/"二、處理過程
出現(xiàn)4xx或5xx錯誤時,ErrorPageCustomizer生效,就會來到/error請求,就會被BasicErrorController處理。
//在DefaultErrorViewResolver中有一段代碼
// 處理4xx和5xx的請求
static {
Map<Series, String> views = new EnumMap(Series.class);
views.put(Series.CLIENT_ERROR, "4xx");
views.put(Series.SERVER_ERROR, "5xx");
SERIES_VIEWS = Collections.unmodifiableMap(views);
}
// 解析,并會跳轉(zhuǎn)到error/錯誤狀態(tài)碼; 頁面中
private ModelAndView resolve(String viewName, Map<String, Object> model) {
String errorViewName = "error/" + viewName;
TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext);
// 對是否有模板引擎做出相應(yīng)的視圖處理
return provider != null ? new ModelAndView(errorViewName, model) : this.resolveResource(errorViewName, model);
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文搞懂SpringBoot如何利用@Async實現(xiàn)異步調(diào)用
異步調(diào)用幾乎是處理高并發(fā),解決性能問題常用的手段,如何開啟異步調(diào)用?SpringBoot中提供了非常簡單的方式,就是一個注解@Async。今天我們重新認(rèn)識一下@Async,以及注意事項2022-09-09
Java?任務(wù)調(diào)度框架?Quartz實操
這篇文章主要介紹了Java?任務(wù)調(diào)度框架?Quartz,Quartz是OpenSymphony開源組織在Job?scheduling領(lǐng)域又一個開源項目,完全由Java開發(fā),可以用來執(zhí)行定時任務(wù),類似于java.util.Timer。,下面我們來學(xué)習(xí)一下關(guān)于?Quartz更多的詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-12-12
Java Date類常用示例_動力節(jié)點Java學(xué)院整理
在JDK1.0中,Date類是唯一的一個代表時間的類,但是由于Date類不便于實現(xiàn)國際化,所以從JDK1.1版本開始,推薦使用Calendar類進(jìn)行時間和日期處理。這里簡單介紹一下Date類的使用,需要的朋友可以參考下2017-05-05

