配置系统异常处理器
1.后台模拟一个异常
@RequestMapping(value = "/myexception.do", produces = "text/html;charset=utf-8") public String myexception() { int a=5/0; return "/error.jsp"; }
2.未配置系统异常时,前台访问报错500,配置系统异常处理器后成功进入错误页面
3.前台页面
错误页面${ex.message}
效果图:
配置自定义异常处理器
1.创建自定义异常类
public class MyException extends Exception{ public MyException() { super(); } public MyException(String message) { super(message); }}
public class NameException extends MyException { public NameException() { super(); } public NameException(String message) { super(message); }}
public class AgeException extends MyException { public AgeException() { super(); } public AgeException(String message) { super(message); }}
2.配置处理器方法
@RequestMapping(value = "/exception.do", produces = "text/html;charset=utf-8") public String exception(String name, Integer age) throws NameException, AgeException { if (!name.equals("admin")) { throw new NameException("用户名错误"); } if (age > 40) { throw new AgeException("年龄太大"); } return "/result.jsp"; }
3.配置applicationContext.xml
/customError/userNameError.jsp /customError/userAgeeError.jsp
配置完成,再出现异常时就对进入对应的错误页面