一、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";
}
}
参考文章: