全局异常处理⽅式⼀:
1、配置简单异常处理器
配置 SimpleMappingExceptionResolver 对象:
<!-- 配置全局异常统⼀处理的 Bean (简单异常处理器) -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- ⻚⾯在转发时出现异常,设置默认的错误⻚⾯ (error代表的是⼀个视图) -->
<property name="defaultErrorView" value="error"></property>
<!-- 异常发⽣时,设置异常的变量名 -->
<property name="exceptionAttribute" value="ex"></property>
</bean>
可以在处理异常的⻚⾯获取异常信息
${ex}
2、 使⽤⾃定义异常
1.参数异常:
/**
* ⾃定义异常:参数异常
*/
public class ParamsException extends RuntimeException {
private Integer code = 300 ;
private String msg = " 参数异常 !" ;
public ParamsException () {
super ( " 参数异常 !" );
}
public ParamsException ( String msg ) {
super ( msg );
this . msg = msg ;
}
public ParamsException ( Integer code ) {
super ( " 参数异常 !" );
this . code = code ;
}
public ParamsException ( Integer code , String msg ) {
super ( msg );
this . code = code ;
this . msg = msg ;
}
public Integer getCode () {
return code ;
}
public void setCode ( Integer code ) {
this . code = code ;
}
public String getMsg () {
return msg ;
}
public void setMsg ( String msg ) {
this . msg = msg ;
}
}
2.业务异常:
/**
* ⾃定义异常:业务异常
*/
public class BusinessException extends RuntimeException {
private Integer code = 400 ;
private String msg = " 业务异常 !" ;
public BusinessException () {
super ( " 业务异常 !" );
}
public BusinessException ( String msg ) {
super ( msg );
this . msg = msg ;
}
public BusinessException ( Integer code ) {
super ( " 业务异常 !" );
this . code = code ;
}
public BusinessException ( Integer code , String msg ) {
super ( msg );
this . code = code ;
this . msg = msg ;
}
public Integer getCode () {
return code ;
}
public void setCode ( Integer code ) {
this . code = code ;
}
public String getMsg () {
return msg ;
}
public void setMsg ( String msg ) {
this . msg = msg ;
}
}
3.、设置⾃定义异常与⻚⾯的映射
<!-- 设置⾃定义异常与⻚⾯的映射 -->
<property name="exceptionMappings">
<props>
<!-- key:⾃定义异常对象的路径; 标签中设置具体的处理⻚⾯的视图名-->
<prop key="com.xxxx.ssm.exception.BusinessException">error</prop>
<prop key="com.xxxx.ssm.exception.ParamsException">error</prop>
</props>
</property>
使⽤ SimpleMappingExceptionResolver 进⾏异常处理,具有集成简单、有良好的扩展性、对已有代码 没有⼊侵性等优点,但该⽅法仅能获取到异常信息,若在出现异常时,对需要获取除异常以外的数据的情况不适⽤。
全局异常处理方式二(推荐):
1、实现 HandlerExceptionResolver 接⼝
/**
* 全局异常统⼀处理
*/
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object handler, Exception ex) {
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex","默认错误信息");
return mv;
}
}
.2. ⾃定义异常处理
/**
* 全局异常统⼀处理
*/
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object handler, Exception ex) {
ModelAndView mv = new ModelAndView("error");
mv.addObject("ex","默认错误信息");
// 判断是否是⾃定义异常
if (ex instanceof ParamsException) {
mv.setViewName("params_error");
ParamsException e = (ParamsException) ex;
mv.addObject("ex", e.getMsg());
}
if (ex instanceof BusinessException) {
mv.setViewName("business_error");
BusinessException e = (BusinessException) ex;
mv.addObject("ex", e.getMsg());
}
return mv;
}
}
使⽤实现 HandlerExceptionResolver 接⼝的异常处理器进⾏异常处理,具有集成简单、有良好的扩展
性、对已有代码没有⼊侵性等优点,同时,在异常处理时能获取导致出现异常的对象,有利于提供更详
细的异常处理信息。
全局异常处理⽅式三:
⻚⾯处理器继承 BaseController:
public class BaseController {
@ExceptionHandler
public String exc(HttpServletRequest request,HttpServletResponse
response,Exception ex){
request.setAttribute("ex", ex);
if(ex instanceof ParamsException){
return "error_param";
}
if(ex instanceof BusinessException){
return "error_business";
}
return "error";
}
}
使⽤ @ExceptionHandler 注解实现异常处理,具有集成简单、有扩展性好(只需要将要异常处理的
Controller 类继承于 BaseController 即可)、不需要附加 Spring 配置等优点,但该⽅法对已有代码存在⼊
侵性 ( 需要修改已有代码,使相关类继承于 BaseController) ,在异常处理时不能获取除异常以外的数据。
未捕获异常的处理:
对于 Unchecked Exception ⽽⾔,由于代码不强制捕获,往往被忽略,如果运⾏期产⽣了Unchecked Exception,⽽代码中⼜没有进⾏相应的捕获和处理,则我们可能不得不⾯对尴尬的 404、 500......等服 务器内部错误提示⻚⾯。
此时需要⼀个全⾯⽽有效的异常处理机制。⽬前⼤多数服务器也都⽀持在 web.xml 中通过(Websphere/Weblogic)或者(Tomcat)节点配置特定异常情况的显示⻚⾯。修改 web.xml ⽂件,增加以下内 容:
<!-- 出错⻚⾯定义 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>