美文网首页
spring异常处理方式之一

spring异常处理方式之一

作者: 又双叒叕苟了一天 | 来源:发表于2018-01-30 19:35 被阅读0次

1.构建异常类

public class CustomeException extends RuntimeException{
  public CustomeException() {
    super();
    // TODO Auto-generated constructor stub
  }

  public CustomeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    super(message, cause, enableSuppression, writableStackTrace);
    // TODO Auto-generated constructor stub
  }

  public CustomeException(String message, Throwable cause) {
    super(message, cause);
    // TODO Auto-generated constructor stub
  }

  public CustomeException(String message) {
    super(message);
    // TODO Auto-generated constructor stub
  }

  public CustomeException(Throwable cause) {
    super(cause);
    // TODO Auto-generated constructor stub
  }
}

2.实现HandlerExceptionResolver接口

public class GlobalExceptionResolver implements HandlerExceptionResolver{
  @Override 
  public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse rep, Object obj,         Exception exc) {
    String msg="";      
    if(exc instanceof CustomeException){            
      msg="客户异常";
    } else {    
      msg="管理员";
    }   
    ModelAndView mav=new ModelAndView();
    mav.addObject("msg",msg);
    mav.setViewName("error");
    return mav;
  }
}

3.配置springMvc.xml文件

<bean class="HandlerExceptionResolver实现类路径">

相关文章

网友评论

      本文标题:spring异常处理方式之一

      本文链接:https://www.haomeiwen.com/subject/zothzxtx.html