Ssm exception handling framework

Exception handling ideas

The system includes two types of abnormalities: abnormal runtimeexception expected exceptions and run-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.

dao system, service, controller throws Exception occurrence is thrown up by the last referred to by the front end controller springMVC exception handler for exception handling, as shown below:

 

 

 springMVC provide a global exception handler unified exception handling, a system is only one exception handler

1 custom exception class

The definition of different types of abnormal exception classes, inheritance Exception

/**
* Created by Alex on 2017/6/29.
* 系统自定义异常类
*/
public class CustomException extends Exception {
//异常信息
  public String message;
  public CustomException(String message){
    super(message);
    this.message = message;
  }

  @Override
  public String getMessage() {
    return message;
  }

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

2 to configure the global exception handler

Ideas:

When the system encounters an exception, in the program manually throw, dao threw service, service thrown controller, controller finally thrown to the front controller, front controller calls a global exception handler.

Global exception handler ideas:

Resolve exception type, if the exception type is a system-defined exception exception information taken directly to the error page display.

If the system is not a custom exception, construct a custom exception type system, information is "unknown error"

springMVC provide a HandlerExceptionResolver

/ ** 
 * ON 2017/6/29 the Created by Alex. 
 * Global exception handlers 
 * / 
public  class CustomExceptionResolver the implements the HandlerExceptionResolver {
     / ** 
     * system exceptions thrown 
     * @param HttpServletRequest 
     * @param HttpServletResponse 
     * @param O 
     * @ param E system exceptions thrown 
     * @return 
     * / 
    @Override 
    public ModelAndView the resolveException (the HttpServletRequest HttpServletRequest, HttpServletResponse the HttpServletResponse, Object O, exception E) {
        // parse the exception type
        CustomException = customException null ;
         // if the exception type is a system-defined abnormality, the abnormality information taken directly to the error page display. 
        IF (E the instanceof customException) { 
            customException = (customException) E; 
        } the else {
             // If the system is not a custom exception, construct a custom exception type system, information is "Unknown error" 
            customException = new new customException ( "Unknown error" ); 
        } 
        // error message 
        String message = customException.getMessage (); 
        ModelAndView ModelAndView = new new ModelAndView ();
         //The error information to the page 
        modelAndView.addObject ( "the Message" , the Message);
         // point to the wrong page 
        modelAndView.setViewName ( "error" );
         return ModelAndView; 
    } 
}

Page 3 configuration errors

<%--
  Created by IntelliJ IDEA.
  User: Alex
  Date: 2017/6/29
  Time: 20:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>失败!</title>
</head>
<body>
${message}
</body>
</html>

Global exception handler configured 4springMVC.xml

<! - 
    global exception handler 
    long HandlerExceptionResolver class implements the interface, is a global exception handler Oh
     -> 
    <the bean class = "com.alex.ssm.exception.CustomExceptionResolver" />

note:

System custom exception, the proposed project and then add all of the functions are completed.

 

Guess you like

Origin www.cnblogs.com/liu1275271818/p/11495228.html