基于springmvc的异常处理
来源:互联网 发布:北控集团工资待遇知乎 编辑:程序博客网 时间:2024/06/02 06:05
在实际的开发过程中,无论是业务还是数据库都有可能会发生异常。如果每个地方都单独去处理异常,系统的代码耦合度高,工作量大且不好统一,维护的工作量也很大。因此,我们将所有类型的异常处理从各处理过程解耦出来,这样既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护。
本文主要讲述基于实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器。
1、web.xml配置异常页面
<error-page><error-code>401</error-code><location>/errorpages/error.jsp</location></error-page><error-page><error-code>403</error-code><location>/errorpages/error.jsp</location></error-page><error-page><error-code>404</error-code><location>/errorpages/error.jsp</location></error-page><error-page><error-code>405</error-code><location>/errorpages/error.jsp</location></error-page><error-page><error-code>500</error-code><location>/errorpages/error.jsp</location></error-page><error-page><error-code>400</error-code><location>/errorpages/error.jsp</location></error-page><error-page><exception-type>java.lang.Exception</exception-type><location>/errorpages/error.jsp</location></error-page>此处用于配置未捕获的异常
2、自定义注解实现方法内部错误时页面跳转和提示
创建自定义注解
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MethodExceptionView {//页面发现异常时跳转的页面String jumpView() default "forward:/errorpages/error.jsp";//页面的提示信息String message() default "操作失败,请刷新再试!";}
3、实现HandlerExceptionResolver接口自定义异常处理类
public class ExceptionHandlerResolver implements HandlerExceptionResolver {private final Log log = LogFactory.getLog(getClass());public ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response, Object handler, Exception exception) {try {String message = null;if (log.isTraceEnabled()) {log.error(exception, exception);} else {log.error(exception);}//异常日志输出StackTraceElement stackTraceElement= exception.getStackTrace()[0];log.error("请求url:" + request.getServletPath());log.error("异常类:" + exception.getClass().getName());log.error("File="+stackTraceElement.getFileName());log.error("Line="+stackTraceElement.getLineNumber());log.error("Method="+stackTraceElement.getMethodName());//ajax请求,返回提示信息if (isAjax(request)) {message = exception.getMessage();addPromptMessageWithAjaxException(request, response, message);return null;}//不处理项目自定义的异常if(exception instanceof AppException || exception instanceof BusinessException) {return new ModelAndView("forward:/errorpages/error.jsp");}//同步请求,代码内部错误时,通过获取方法上的自定义注解进行 页面跳转和信息提示HandlerMethod handlerMethod = (HandlerMethod) handler; MethodExceptionView methodExceptionView = handlerMethod.getMethod().getAnnotation(MethodExceptionView.class);if(null != methodExceptionView) {ModelAndView mav = new ModelAndView();mav.setViewName(methodExceptionView.jumpView());mav.addObject("message", methodExceptionView.message());return mav;}} catch (Exception e) {}return new ModelAndView("forward:/errorpages/error.jsp");}private void addPromptMessageWithAjaxException(HttpServletRequest request, HttpServletResponse response, String message) {Writer writer = null;JSONObject jsonObject = new JSONObject();jsonObject.put("success", false);jsonObject.put("message", message);System.out.println(message);try {writer = response.getWriter();writer.write(jsonObject.toJSONString());writer.flush();writer.close();} catch (IOException e) {log.error("io close exception", e);} catch (Exception e) {log.error("io close exception", e);}finally {try {writer.flush();writer.close();} catch (IOException e) {}}}/** * * 验证是否为ajax请求 * @param request * @return */private boolean isAjax(HttpServletRequest request) {return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));}}
此处除了处理异常外还记录了异常请求和异常产生处,(注:此处无法捕获异常)
0 0
- 基于springmvc的异常处理
- springMvc基于注解的异常处理
- SpringMVC的异常处理
- springMVC的异常处理
- SpringMvc的异常处理
- SpringMVC的异常处理
- springMvc的异常处理
- SpringMVC 异常的处理
- SpringMVC的异常处理
- SpringMVC的异常处理
- springmvc-异常的处理
- springMVc对异常的处理
- springMVC全局的异常处理
- SpringMVC的异常处理方式
- springmvc+mybatis 的异常处理
- springmvc的自定义异常处理
- Springmvc 异常处理的优先级
- springMVC的统一异常处理
- 【那些工作中遇到的小问题】读写锁
- 泛型
- PHP学习 Class one
- python 去除大文件所有行全为零,所有列全为零的行和列
- navicat for mysql
- 基于springmvc的异常处理
- 酷夏解暑神果——你造吗
- 数据库交互ActiveRecord
- android 之@+id与@id以及引用中用@+id/的原因
- 考古界共识:秦始皇陵地宫不能挖
- PHP微信第三方扫码登录技术问题
- jQuery选择器
- Java基础_解决问题1,随机数问题
- 使用jQuery对List排序