SpringMVC的处理器全局异常处理类
package com.huawei.utils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
/*
* @param httpServletRequest
* @param httpServletResponse
* @param o 在那个对象中出现异常
* @param e 异常对象
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if(ex instanceof UnauthorizedException) {
return new ModelAndView("forward:/unauthorized.jsp");
}else {
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("errorMsg", "对不起,我错了");
mv.addObject("ex", ex);
return mv;
}
}
}