java开发之异常处理_SimpleMappingExceptionResolver
异常处理_SimpleMappingExceptionResolver1)如果希望对所有异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常1实验代码增加页面链接<a href=”testSimpleMappingExceptionResolver?i=1″>testSimpleMappi
异常处理_SimpleMappingExceptionResolver
1)如果希望对所有异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异
常类名映射为视图名,即发生异常时使用对应的视图报告异常
1实验代码
- 增加页面链接
<a href=”testSimpleMappingExceptionResolver?i=1″>testSimpleMappingExceptionResolver</a> |
- 增加控制器方法
@RequestMapping(“/testSimpleMappingExceptionResolver”) public String testSimpleMappingExceptionResolver(@RequestParam(“i”) int i){ System.out.println(“testSimpleMappingExceptionResolver…”); String[] s = new String[10]; System.out.println(s[i]); return “success”; } |
- 出现异常情况:参数i的值大于10
- 配置异常解析器:自动将异常对象信息,存放到request范围内
<!– 配置SimpleMappingExceptionResolver异常解析器 –> <bean id=”simpleMappingExceptionResolver” class=”org.springframework.web.servlet.handler.SimpleMappingExceptionResolver”> <!– exceptionAttribute默认值(通过ModelAndView传递给页面): exception -> ${requestScope.exception} public static final String DEFAULT_EXCEPTION_ATTRIBUTE = “exception”; –> <property name=”exceptionAttribute” value=”exception”></property> <property name=”exceptionMappings“> <props> <prop key=”java.lang.ArrayIndexOutOfBoundsException”>error</prop> </props> </property> </bean> |
error.jsp |
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd“> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>Insert title here</title> </head> <body> <h3>Error Page</h3> ${exception } ${requestScope.exception } </body> </html> |
- 源码分析
SimpleMappingExceptionResolver L187 L339 |
@Override protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,Object handler, Exception ex) {
// Expose ModelAndView for chosen error view. String viewName = determineViewName(ex, request); if (viewName != null) { // Apply HTTP status code for error views, if specified. // Only apply it if we’re processing a top-level request. Integer statusCode = determineStatusCode(request, viewName); if (statusCode != null) { applyStatusCodeIfPossible(request, response, statusCode); } return getModelAndView(viewName, ex, request); }else { return null; } } |
/** * Return a ModelAndView for the given view name and exception. * <p>The default implementation adds the specified exception attribute. * Can be overridden in subclasses. * @param viewName the name of the error view * @param ex the exception that got thrown during handler execution * @return the ModelAndView instance * @see #setExceptionAttribute */ protected ModelAndView getModelAndView(String viewName, Exception ex) { ModelAndView mv = new ModelAndView(viewName); if (this.exceptionAttribute != null) { if (logger.isDebugEnabled()) { logger.debug(“Exposing Exception as model attribute ‘” + this.exceptionAttribute + “‘”); } mv.addObject(this.exceptionAttribute, ex); } return mv; } |
原文链接:http://www.atguigu.com/jsfx/10316.html
更多推荐
所有评论(0)