springmvc学习笔记(30)——SimpleMappingExceptionResolver异常映射

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

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> 
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

写个目标方法测试一下

    @RequestMapping("/testExceptionMapping")    public String testExceptionMapping(){        int arrays[] = new int[10];        System.out.println(arrays[11]);        return "hello";    }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这里将发生数组下标越界的异常,访问目标方法,得到如下结果
这里写图片描述

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>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

so easy .哪里不会点哪里

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_43678306/article/details/84071086