1. 程式人生 > >SpringMVC總結之異常處理的三種方式

SpringMVC總結之異常處理的三種方式

目錄

1.使用Spring MVC提供的簡單異常處理器SimpleMappingExceptionResolver

2.實現Spring的異常處理介面HandlerExceptionResolver 自定義自己的異常處理器

3.使用@ExceptionHandler註解實現異常處理


1.使用Spring MVC提供的簡單異常處理器SimpleMappingExceptionResolver

 1.1 在springmvc配置中配置:


    <!-- 總錯誤處理-->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="defaultErrorView">     
            <value>/error/error</value>  
        </property>  
        <property name="defaultStatusCode">     
            <value>500</value>  
        </property>      
    <property name="warnLogCategory">     
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>  
        </property>      
    </bean>   

[xml] view plaincopy

    <!-- 總錯誤處理-->  
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="defaultErrorView">    
            <value>/error/error</value>  
        </property>  
        <property name="defaultStatusCode">    
            <value>500</value>  
        </property>     
    <property name="warnLogCategory">    
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver</value>  
        </property>     
    </bean>   

 1.2 顯示錯誤的jsp頁面:


    <%@ page language="java" contentType="text/html; charset=GBK"  
        pageEncoding="GBK"%>  
    <%@ page import="java.lang.Exception"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
    <title>錯誤頁面</title>  
    </head>  
    <body>  
    <h1>出錯了</h1>  
    <%   
    Exception e = (Exception)request.getAttribute("exception");   
    out.print(e.getMessage());   
    %>  
    </body>  
    </html>  

[html] view plaincopy

    <%@ page language="java" contentType="text/html; charset=GBK"  
        pageEncoding="GBK"%>  
    <%@ page import="java.lang.Exception"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
    <title>錯誤頁面</title>  
    </head>  
    <body>  
    <h1>出錯了</h1>  
    <%  
    Exception e = (Exception)request.getAttribute("exception");  
    out.print(e.getMessage());  
    %>  
    </body>  
    </html>  

2.實現Spring的異常處理介面HandlerExceptionResolver 自定義自己的異常處理器

@Component  
public class ExceptionTest implements HandlerExceptionResolver{  

    /**  
     * TODO 簡單描述該方法的實現功能(可選).  
     * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)  
     */   
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  
            Exception ex) {  
        System.out.println("This is exception handler method!");  
        return null;  
    }  
}

3.使用@ExceptionHandler註解實現異常處理

@Controller      
public class GlobalController {               

   /**    
     * 用於處理異常的    
     * @return    
     */      
    @ExceptionHandler({MyException.class})       
    public String exception(MyException e) {       
        System.out.println(e.getMessage());       
        e.printStackTrace();       
        return "exception";       
    }       

    @RequestMapping("test")       
    public void test() {       
        throw new MyException("出錯了!");       
    }                    
}

附:https://www.cnblogs.com/junzi2099/p/7840294.html#_label0