异常处理方式
* 配置简单异常处理器SimpleMappingExceptionResolver
* 配置自定义异常处理器
自定义异常处理步骤
* 创建异常处理器实现HandlerExceptionResolver
public class MyExceptionResolver implements HandlerExceptionResolver{ /* * 参数Exception:异常对象 * 返回值ModelAndView:跳转到错误视图信息 */ public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView = new ModelAndView(); if(ex instanceof MyException) { modelAndView.addObject("info","自定义异常"); } else if(ex instanceof ClassCastException) { modelAndView.addObject("info","类转换异常"); } modelAndView.setViewName("error"); return modelAndView; }
* 配置异常处理器
<!-- 配置简单异常处理器 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value = "error"></property> <property name="exceptionMappings"> <map> <entry key = "java.lang.ClassCastException" value="error1"></entry> <entry key = "com.zl.exception.MyException" value="error2"></entry> </map> </property> </bean>--> <!-- 自定义异常 --> <bean class = "com.zl.resolver.MyExceptionResolver"></bean>
* 编写异常页面
* 测试异常跳转
标签:自定义,SpringMVC,处理,处理器,modelAndView,ModelAndView,异常,ex From: https://www.cnblogs.com/zlyyds/p/16935002.html