SpringMVC------异常处理

 Spring MVC 应用的开发中,不管是对底层数据库操作,还是业务层或控制层操作,都会不可避免地遇到各种可预知的、不可预知的异常需要处理。

如果每个过程都单独处理异常,那么系统的代码耦合度高,工作量大且不好统一,以后维护的工作量也很大。

如果能将所有类型的异常处理从各层中解耦出来,这样既保证了相关处理过程的功能单一,又实现了异常信息的统一处理和维护。

幸运的是,Spring MVC 框架支持这样的实现。Spring MVC 统一异常处理有以下 3 种方式:

  • 使用 Spring MVC 提供的简单异常处理器 SimpleMappingExceptionResolver。
  • 实现 Spring 的异常处理接口 HandlerExceptionResolver 自定义自己的异常处理器。
  • 使用 @ExceptionHandler 注解实现异常处理


本节主要根据这 3 种处理方式讲解 Spring MVC 应用的异常统一处理。

 

Spring MVC使用SimpleMappingExceptionResolver类异常处理

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring一beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    base-package="controller" />
    base-package="service" />
    base-package="dao" />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="internalResourceViewResolver">
        
        "prefix" value="/WEB-INF/jsp/" />
        
        "suffix" value=".jsp" />
    
    
    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        
        "defaultErrorView" value="error">
        
        "exceptionAttribute" value="ex">
        
        "exceptionMappings">
            
                "exception.MyException">my-error
                "java.sql.SQLException">sql-error
                
            
        
    

Spring MVC使用HandlerExceptionResolver接口异常处理

 

package exception;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest arg0,
            HttpServletResponse arg1, Object arg2, Exception arg3) {
        Map model = new HashMap();
        model.put("ex", arg3);
        // 根据不同错误转向不同页面(统一处理),即异常与View的对应关系
        if (arg3 instanceof MyException) {
            return new ModelAndView("my-error", model);
        } else if (arg3 instanceof SQLException) {
            return new ModelAndView("sql-error", model);
        } else {
            return new ModelAndView("error", model);
        }
    }
}
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring一beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    base-package="controller" />
    base-package="service" />
    base-package="dao" />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="internalResourceViewResolver">
        
        "prefix" value="/WEB-INF/jsp/" />
        
        "suffix" value=".jsp" />
    
    
    class="exception.MyExceptionHandler"/>

Spring MVC使用@ExceptionHandler注解异常处理

package controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import exception.MyException;
public class BaseController {
    /** 基于@ExceptionHandler异常处理 */
    @ExceptionHandler
    public String exception(HttpServletRequest request, Exception ex) {
        request.setAttribute("ex", ex);
        // 根据不同错误转向不同页面,即异常与view的对应关系
        if (ex instanceof SQLException) {
            return "sql-error";
        } else if (ex instanceof MyException) {
            return "my-error";
        } else {
            return "error";
        }
    }
}
"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring一beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    base-package="controller" />
    base-package="service" />
    base-package="dao" />
    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="internalResourceViewResolver">
        
        "prefix" value="/WEB-INF/jsp/" />
        
        "suffix" value=".jsp" />
    

 

你可能感兴趣的:(SpringMVC------异常处理)