Global 13.SpringMVC of abnormal

We know that the system includes an exception: Exception RuntimeException exceptions and run compile time, the former in order to gain an exception by capturing exception information, the latter mainly through the standard code development, testing by means of reducing the occurrence of abnormal operation. In development, whether dao layer, service layer or the controller layer, are likely to throw an exception, in springmvc in bringing all types of exception handling decoupled from each process, both to ensure the functions related to the processing of relatively single, unified treatment also achieved and maintained exception information. This blog is mainly summarize how unified SpringMVC handle exceptions.

1. The idea of ​​exception handling

  First look at the ideas in springmvc in exception handling (I have tried to draw a nice point, do not spray me ~): 
springmvc Exception Handling 
  As shown above, dao system, service, controller abnormal throws Exception are thrown up by the last referred to by the exception handler controller springmvc distal exception handling. springmvc provide global exception handler (a system is only one exception handler) unified exception handling. Understand springmvc in exception handling mechanism, the following analysis springmvc began in exception handling.

2. springmvc comes with a simple exception handler

  springmvc comes with an exception handler called SimpleMappingExceptionResolver, the processor implements the interface HandlerExceptionResolver global exception handler needs to implement this interface. We want to use the built-in exception handler must first configure the processor springmvc.xml file:

<! - simple exception handler springmvc offer -> 
< bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" > 
     <! - defined default exception handler page -> 
    < Property name = " defaultErrorView " value =" / the WEB-INF / JSP / error.jsp " /> 
    <-! defined exception handling variable names used to get the page exception information, or not defined, default name Exception ->  
    < Property name = "exceptionAttribute" value = "EX" /> 
    <-! definition requires special handling of exception, this is an important point ->  
    < Property name = "exceptionMappings">
        <props> 
            < Prop Key = "ssm.exception.CustomException" > /WEB-INF/jsp/custom_error.jsp </ prop > 
        </ The props > 
        <-! May also define other custom exception -> 
    </ Property > 
</ the bean >

From the above configuration point of view, the most important thing is to configure an exception special treatment, these abnormalities are usually our custom, customize according to the actual situation abnormal, and then will jump to a different page to display different error display error message. Here with a custom exception CustomException to illustrate the problem, defined as follows:

// define a simple exception class 
public  class customException the extends Exception { 

    // abnormality information 
    public String Message; 

    public customException (String Message) {
         Super (Message);
         the this .message = Message; 
    } 

    public String the getMessage () {
         return Message; 
    } 

    public  void the setMessage (String Message) {
         the this .message = Message; 
    } 

}

The next step is to write test procedures, or use the query example as follows: 
abnormal

3. custom global exception handler

springmvc a HandlerExceptionResolver interface custom global exception handler must implement this interface, as follows:

public class CustomExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex) {

        ex.printStackTrace();
        CustomException customException = null;

        //如果抛出的是系统自定义的异常则直接转换
        if(ex instanceof CustomException) {
            customException = (CustomException) ex;
        } else {
            //如果抛出的不是系统自定义的异常则重新构造一个未知错误异常
            //这里我就也有CustomException省事了,实际中应该要再定义一个新的异常
            customException = new CustomException("系统未知错误");
        }

        //向前台返回错误信息
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", customException.getMessage());
        modelAndView.setViewName("/WEB-INF/jsp/error.jsp");

        return modelAndView;
    }
}

全局异常处理器中的逻辑很清楚,我就不再多说了,然后就是在springmvc.xml中配置这个自定义的异常处理器:

 

<! - custom global exception handler 
as long as the interface is implemented HandlerExceptionResolver global exception handler -> 
< the bean class = "ssm.exception.CustomExceptionResolver" > </ the bean > 

4. @ExceptionHandler implement exception handling annotations

  Another method is to use annotations, I say something about the idea, because the method is relatively large invasion of the code, I do not like this way. 
  First, write a BaseController class, and class methods used @ExceptionHandler annotation statement exception handling, such as:

public class BaseController { 
    @ExceptionHandler  
    public String exp(HttpServletRequest request, Exception ex) { 
    //异常处理
    //......
    }
}

Then all Controller needs exception handling inherit this BaseController, although from the execution point of view, no need to configure anything, but the code is intrusive, Controller needs exception handling it should inherit the job. 

Guess you like

Origin www.cnblogs.com/deityjian/p/11498925.html