springmvc的全局异常处理器开发

1.自定义异常类CustomException

   对不同的异常类型定义异常类继承EXCEPTION

   

  1. package cn.ljj.exception;


    /**
     * 
     * <p>Title: CustomException</p>
     * <p>Description:系统 自定义异常类,针对预期的异常,需要在程序中抛出此类的异常 </p>
     * @author ljj
     * @date 2015-4-14上午11:52:02
     * @version 1.0
     */
    public class CustomException extends Exception {

    //异常信息
    public String message;

    public CustomException(String message){
    super(message);
    this.message = message;
    }


    public String getMessage() {
    return message;
    }


    public void setMessage(String message) {
    this.message = message;
    }




    }

  2.异常类型分析

              

3.定义自定义异常解析器

  1. package cn.ljj.exception;


    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;


    /**
     * 
     * <p>Title: CustomExceptionResolver</p>
     * <p>Description:全局异常处理器 </p>
     * @author ljj
     * @date 2015-4-14上午11:57:09
     * @version 1.0
     */
    public class CustomExceptionResolver implements HandlerExceptionResolver {


    /**
    * (非 Javadoc)
    * <p>Title: resolveException</p>
    * <p>Description: </p>
    * @param request
    * @param response
    * @param handler
    * @param ex 系统 抛出的异常
    * @return
    * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
    */
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex) {
    //handler就是处理器适配器要执行Handler对象(只有method)

    // 解析出异常类型
    // 如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示
    // String message = null;
    // if(ex instanceof CustomException){
    // message = ((CustomException)ex).getMessage();
    // }else{
    如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
    // message="未知错误";
    // }

    //上边代码变为
    CustomException customException = null;
    if(ex instanceof CustomException){
    customException = (CustomException)ex;
    }else{
    customException = new CustomException("未知错误");
    }

    //错误信息
    String message = customException.getMessage();


    ModelAndView modelAndView = new ModelAndView();

    //将错误信息传到页面
    modelAndView.addObject("message", message);

    //指向错误页面
    modelAndView.setViewName("error");



    return modelAndView;
    }


    }

     4.在springmvc.xml中配置全局异常处理器

  1. <!-- 全局异常处理器
    只要实现HandlerExceptionResolver接口就是全局异常处理器
    -->
    <bean class="cn.ljj.exception.CustomExceptionResolver"></bean>

      5.只要是系统异常都可以通过throw new  CustomException("相对应的message")抛出

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值