springMVC统一处理异常

转自:http://hi.baidu.com/99999999hao/blog/item/25da70174bfbf642f919b8c3.html

 

1、实现spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器。

package com.liangzhi.exception;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

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

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

public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

 public ModelAndView resolveException(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex) {    
        Map model = new HashMap();
        model.put("ex", ex.getClass().getSimpleName());
        model.put("error", ex.getMessage());
        return new ModelAndView("/error", model);
    }
}

  

 

2、在配置文件里配置这个类。

<bean id="myHandlerExceptionResolver" class="com.liangzhi.exception.MyHandlerExceptionResolver"/>

  

 

3、在工程下建error.jsp页面,用来显示出错时,要显示的信息。

你可能感兴趣的:(springMVC)