java培训课程之异常处理_SimpleMappingExceptionResolver

异常处理_SimpleMappingExceptionResolver

1)如果希望?#36816;?#26377;异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异

??常类名映射为视图名,即发生异常时使用对应的视图报告异常

Java培训

? ? ?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

java培训

  • 配置异常解析器:自动将异常对象信息,存放到request范围内

<!– 配置SimpleMappingExceptionResolver异常解析器 –>

<bean id=”simpleMappingExceptionResolver”

?class=”org.springframework.web.servlet.handler.SimpleMappingExceptionResolver”>

<!– exceptionAttribute默?#29616;?通过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>

java培训
  • 源码分析

SimpleMappingExceptionResolver??? L187 L339?

java培训

@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;

}

?


上一篇:
下一篇:
关于我们
名师团队
新闻动态
合作企业
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
电话:010-56253825
邮箱:[email protected]
地址:北京市昌平区宏福科技园综?#19979;?层(北京校区)

 深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

轩辕传奇通天塔
新疆时时走势图下载 内蒙古时时视频 3d走势图试机号开机号 im体育公司在哪里 牛彩网3d字谜总汇九l 北京pk直播 网易手游官网阴阳师 江苏七位数和值走势图 什么平台能玩黑杰克 页游变态版