springmvc统一异常处理
2016-07-04 12:05
501 查看
![](https://images2015.cnblogs.com/blog/556277/201607/556277-20160704120119077-1530719022.png)
![](https://images2015.cnblogs.com/blog/556277/201607/556277-20160704120300967-630754099.png)
![](https://images2015.cnblogs.com/blog/556277/201607/556277-20160704120359827-157427625.png)
public class CustomExceptionResolver implements HandlerExceptionResolver { //前端控制器DispatcherServlet在进行HandlerMapping、调用HandlerAdapter执行Handler过程中,如果遇到异常就会执行此方法 //handler最终要执行的Handler,它的真实身份是HandlerMethod //Exception ex就是接收到异常信息 @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { //输出异常 ex.printStackTrace(); //统一异常处理代码 //针对系统自定义的CustomException异常,就可以直接从异常类中获取异常信息,将异常处理在错误页面展示 //异常信息 String message = null; CustomException customException = null; //如果ex是系统 自定义的异常,直接取出异常信息 if(ex instanceof CustomException){ customException = (CustomException)ex; }else{ //针对非CustomException异常,对这类重新构造成一个CustomException,异常信息为“未知错误” customException = new CustomException("未知错误"); } //错误 信息 message = customException.getMessage(); request.setAttribute("message", message); try { //转向到错误 页面 request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new ModelAndView(); } }
相关文章推荐
- java选择排序Selection Sort
- java方法延时执行
- Spring整合MyBatis(Maven+MySQL)图文教程详解
- shiro注解配置
- 如何把Eclipse工程导入到Android Studio
- Java对象序列化
- springMVC和spring上下文的关系
- Error when passing date to create solr date format.java.text.ParseException: Unparseable date: "We 4000 d
- [置顶] Java 多线程学习笔记(十) InheritableThreadLocal的使用
- jar 问题 : java.io.IOException: invalid header field
- Ubuntu16.04下面配置java环境变量
- Spring 事物的传播特性
- Ubuntu 16 eclipse 4打开后没有菜单栏的解决方法
- [改善Java代码]枚举项的数量限制在64个以内
- 消息总线——EventBus
- Java Web实战05--Spring之bean引用
- Spring MVC学习总结(6)——一些Spring MVC的使用技巧
- Spring MVC学习总结(6)——一些Spring MVC的使用技巧
- Spring MVC学习总结(6)——一些Spring MVC的使用技巧
- spring @Scheduled注解执行定时任务