SpringMVC中的異常處理
阿新 • • 發佈:2020-07-13
一、編寫自定義異常類(作為提示資訊)
@Data
public class SysException extends Exception {
private String message;
public SysException(String message) {
this.message = message;
}
}
二、編寫異常處理器
public class SysExceptionResolve implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { //處理異常業務邏輯 SysException sys = null; if(e instanceof SysException){ sys = (SysException) e; }else{ //如果丟擲的不是系統自定義異常則重新構造一個系統錯誤異常。 sys = new SysException("系統自定義異常...."); } //攜帶異常資訊跳轉到提示頁面 ModelAndView mv = new ModelAndView(); mv.addObject("errorMsg",sys.getMessage()); mv.setViewName("error"); return mv; } }
三、在springmvc.xml中配置異常處理器(跳轉到提示頁面)