SimpleMappingExceptionResolver的使用

来源:互联网 发布:from unixtime mysql 编辑:程序博客网 时间:2024/06/16 13:51

//处理器方法如下

@RequestMapping("/testSimpleMappingExceptionResolver")
 public String testSimpleMappingExceptionResolver(){
  String[] values=new String[10];
  System.out.println(values[10]); //会报ArrayIndexOutOfBoundsException
  return "success";
 }

SpringMVC的配置文件增加如下内容:

<!-- 配置SimpleMappingExceptionResolver -->
 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  <!-- 在error.jsp中可以使用EL表达式${exception}获取发生的异常值,默认的值就是exception -->
  <property name="exceptionAttribute" value="exception"></property>
  <property name="exceptionMappings">
   <props>
    <!-- 出现java.lang.ArrayIndexOutOfBoundsException异常时,转向逻辑视图error -->
    <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
   </props>
  </property>
 </bean>

这样,访问接口/testSimpleMappingExceptionResolver时,就会转向error.jsp页面。并且在error.jsp页面通过${exception}可以获取刚才发生的异常:java.lang.ArrayIndexOutOfBoundsException: 10

0 0