zoukankan      html  css  js  c++  java
  • springMVC相关—异常处理

    一、基于@ExceptionHandler 注解的异常处理方法: 

     1.加上<mvc:annotation-driven>标签:(该标签是标配,开发时一般都携带)
        2.在当前Handler中定义由@ExceptionHandler注解修饰的方法,用于处理异常信息!

      @ExceptionHandler(value={异常类型.class})

      @ExceptionHandler(value={异常类型.class,异常类型.class,........})
        注意:
        1.@ExceptionHandler方法修饰的入参中可以加入Exception类型的参数,该参数即对应发生的异常信息 

    1         @ExceptionHandler(value={ArithmeticException.class})
    2     public String ExcptionHandler(Exception ex) {
    3         System.out.println(ex);
    4         return "error";
    5     }
    6 //Exception ex作为入参,可以显示异常对象信息到控制台
    示例

        2.@ExceptionHandler方法的入参中不能传入Map.若希望把异常信息传到页面上,需要使用ModelAndView作为方法的返回值。(也不可以用model)

     1 类中:
     2         @ExceptionHandler(value={ArithmeticException.class})
     3     public ModelAndView ExcptionHandler(Exception ex) {
     4         ModelAndView mv=new ModelAndView();
     5         mv.setViewName("error");
     6         mv.addObject("exception", ex);
     7         return mv;
     8     }
     9 错误页面:
    10     <h1>这是一个错误页面!</h1>
    11     ${exception }
    示例

        3.@ExceptionHandler 注解定义的方法优先级问题:
        例如发 生的是NullPointerException,但是声明的异常有 RuntimeException 和 Exception,此候会根据异常的最近 继承关系找到继承深度最浅的那个 @ExceptionHandler 注解方法,即标记了 RuntimeException 的方法

     1  @ExceptionHandler(value={Exception.class})
     2       public void ExcptionHandler1() {
     3           System.out.println("发生异常");    
     4           
     5       }
     6       
     7       @ExceptionHandler(value={ArithmeticException.class})
     8       public String ExcptionHandler() {
     9           System.out.println("发生异常");
    10          return "error";
    11      }
    12      
    13      @RequestMapping(value="/testException")
    14      public String testException(@RequestParam(value="i") Integer i) {
    15          System.out.println(10/i);
    16          return "success";
    17      }  
    18  //注解上的运行异常类型与实际的异常类型比较,运行最准确的哪个(范围小,最贴近)执行异常处理 
    @ExceptionHandler注解多异常方法处理

        4.ExceptionHandlerMethodResolver 内部若找不 到@ExceptionHandler 注解的话,会找@ControllerAdvice 中的@ExceptionHandler 注解方法 

    对所有的handler异常信息处理:

     1 @ControllerAdvice
     2 public class HandlerException {
     3     
     4     @ExceptionHandler(value={ArithmeticException.class})
     5     public String handleException() {
     6         
     7         return "error";
     8     }
     9 }
    10 //所有Handler的ArithmeticException异常均会经过上面函数来进行统一处理
    全局异常处理

     注意:

      在程序执行处理类中有异常处理方法的话,遇到异常程序会优先走本类中的异常处理方法,

        若是没有的话,则会找@ControllerAdvice 注解类中的@ExceptionHandler 注解方法。

        1、@ControllerAdvice 注解类:处理全局异常。

        2、异常处理:总是优先走本类中方法。

     1 jsp页面中:
     2 <a href="${pageContext.request.contextPath }/testException?i=2">异常处理</a>
     3 同一类中:
     4        @ExceptionHandler(value={ArithmeticException.class})
     5     public String ExcptionHandler() {
     6         System.out.println("发生异常");
     7         return "error";
     8     }
     9     
    10     @RequestMapping(value="/testException")
    11     public String testException(@RequestParam(value="i") Integer i) {
    12         System.out.println(10/i);
    13         return "success";
    14     }  
    15 //将网址上i =2改为i =0时发生异常,通过注解的方法转到error页面! 
    @ExceptionHandler异常处理基础示例

    二、基于配置的异常处理:
        如果希望对所有异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异常类名映射为 视图名,即发生异常时使用对应的视图报告异常     

     1  <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     2             <!-- 指定在在request域中获取异常信息所需要的key:即ex -->
     3             <property name="exceptionAttribute" value="ex"></property>
     4             <!-- 指定异常映射 -->
     5             <property name="exceptionMappings">
     6             <!-- property常用语键和值均为字符串类型 -->
     7                 <props>
     8                    <!--  由prop标签的key属性指定发生异常的全类名,由值指定出现异常去哪个页面! -->
     9                     <prop key="java.lang.ArithmeticException">error</prop>
    10                 </props>
    11             </property>
    12         </bean>    
    异常处理配置信息
  • 相关阅读:
    转载:C#中的属性、和字段的区别
    在idea中创建一个maven web工程及解决工程创建过慢问题
    注解&动态代理
    Listener&Filter
    Ajax&jQuery
    JSP&EL&JSTL
    Cookie&Session
    HttpServletRequest&HttpServletResponse
    Http协议&Servlet
    XML
  • 原文地址:https://www.cnblogs.com/kangxingyue-210/p/7425059.html
Copyright © 2011-2022 走看看