- 一般步骤:
-
/** * 异常类 * @author zhou * @create 2020/5/14 */ public class MyException extends Exception { private String message; public MyException(String message) { this.message = message; } @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
-
/** * 自定义异常处理器 * @author zhou * @create 2020/5/14 */ public class MyExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //1.获取异常类对象 MyException e = null; if(ex instanceof MyException) { e = (MyException) ex; }else { e = new MyException("系统正在维护..."); } //2.创建ModelAndView对象 ModelAndView mv = new ModelAndView(); mv.addObject("errorMsg", e.getMessage()); mv.setViewName("error"); return mv; } }
-
<!--配置异常处理器--> <bean id="myExceptionResolver" class="com.dhu.exception.MyExceptionResolver"></bean>
-
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h3>${errorMsg}</h3> </body> </html>