Spring异常处理

一、404、500等未捕获异常处理

修改web.xml配置

<error-page>
         <error-code>404</error-code>
         <location>/WEB-INF/common/error/404.jsp</location>
     </error-page>
     
     <error-page>
         <error-code>500</error-code>
         <location>/WEB-INF/common/error/500.jsp</location>
     </error-page>

二、运行期异常处理

1、Spring提供SimpleMappingExceptionResolver进行异常处理

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="/error" />        
        <property name="exceptionMappings">
            <props>
            	<!-- 数组越界异常,跳到array_error.jsp这个页面 -->
            	<prop key="java.lang.ArrayIndexOutOfBoundsException">/array_error</prop>
            	
            	<!-- 运算异常,跳到arithmetic_error.jsp这个页面 -->
            	<prop key="java.lang.ArithmeticException">/arithmetic_error</prop>
            	
            	<!-- 这里还可以继续扩展对不同异常类型的处理 -->
            	
            	<!-- 只要有异常就跳到error.jsp这个页面 -->
                <prop key="java.lang.Throwable">/error</prop>
            </props>
        </property>
    </bean>

 

使用SimpleMappingExceptionResolver进行异常处理,具有集成简单、有良好的扩展性、对已有代码没有入侵性等优点,但该方法仅能获取到异常信息,若在出现异常时,对需要获取除异常以外的数据的情况不适用。

2、实现HandlerExceptionResolver 接口自定义异常处理器

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 request, HttpServletResponse response, Object handler,
            Exception ex) {
        
        /*** 根据不同错误转向不同页面   ***/
        
        // 数组越界异常,跳到array_error.jsp这个页面
        if (ex instanceof ArrayIndexOutOfBoundsException) 
        {
            return new ModelAndView("array_error", null);
        } 
        // 运算异常,跳到arithmetic_error.jsp这个页面
        else if (ex instanceof ArithmeticException) 
        {
            return new ModelAndView("arithmetic_error", null);
        }
        
        // 其他异常就跳到error.jsp这个页面
        return new ModelAndView("error", null); 
    }

}

 Spring配置文件添加bean:

<bean id="exceptionHandler" class="com.learn.commons.MyExceptionHandler" />


3、使用@ExceptionHandler注解实现异常处理

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {

    @ExceptionHandler
    public String exp(HttpServletRequest request, Exception ex) {
        
        /*** 根据不同错误转向不同页面   ***/
        
        // 数组越界异常,跳到array_error.jsp这个页面
        if (ex instanceof ArrayIndexOutOfBoundsException) 
        {
            return "array_error";
        } 
        // 运算异常,跳到arithmetic_error.jsp这个页面
        else if (ex instanceof ArithmeticException) 
        {
            return "arithmetic_error";
        }
        
        // 其他异常就跳到error.jsp这个页面
        return "error"; 
    }
} 

所有Controller继承BaseController

4、@ControllerAdvice+@ExceptionHandler注解实现异常处理

@ControllerAdvice控制器增强,原理是使用Aop对Controller控制器进行增强。取代了方法3中所有Controller继承BaseController。

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ControllerAdviceExceptionHandler {
		
	@ExceptionHandler
	public String exp(HttpServletRequest request, Exception ex) {
		
		/*** 根据不同错误转向不同页面   ***/
		
		// 数组越界异常,跳到array_error.jsp这个页面
		if (ex instanceof ArrayIndexOutOfBoundsException) 
		{
			return "array_error";
		} 
		// 运算异常,跳到arithmetic_error.jsp这个页面
		else if (ex instanceof ArithmeticException) 
		{
			return "arithmetic_error";
		}
		
		// 其他异常就跳到error.jsp这个页面
		return "error"; 
	}
}


参考文章:

http://blog.csdn.net/ufo2910628/article/details/40399539

http://www.cnblogs.com/nosqlcoco/p/5562107.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值