1.手动设置异常(测试)
@RequestMapping("/myError")
public String myError() {
//手动异常
throw new RuntimeException("运行时异常");
}
2.创建异常处理工具类
public class MyError implements HandlerExceptionResolver { //必须实现接口
@Override
public ModelAndView resolveException(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("error"); //跳转url
modelAndView.addObject("error",e.getMessage());//异常信息
return modelAndView;
}
}
3.配置springmvc.xml
<!--集中处理异常-->
<bean class="com.xxx.error.MyError"></bean>