转自:https://blog.csdn.net/qqqqqq654/article/details/65767701
SimpleMappingException异常映射
当异常发生时,我们可以将它映射到我们指定的界面
在springmvc中配置
<!-- 配置使用 SimpleMappingExceptionResolver 来映射异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!-- 配置异常的属性值为ex,那么在错误页面中可以通过 ${ex} 来获取异常的信息 如果不配置这个属性,它的默认值为exception --> <property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings"> <props> <!-- 映射ArrayIndexOutOfBoundsException异常对应error.jsp这个页面 --> <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop> </props> </property> </bean>
写个目标方法测试一下
@RequestMapping("/testExceptionMapping")
public String testExceptionMapping(){
int arrays[] = new int[10]; System.out.println(arrays[11]); return "hello"; }
这里将发生数组下标越界的异常,访问目标方法,得到如下结果
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> ${ex } </body> </html>
so easy .哪里不会点哪里