SimpleMappingExceptionResolver的使用方法

来源:互联网 发布:蚌埠电视台网络直播 编辑:程序博客网 时间:2024/06/11 21:33

如果希望对所有异常进行统一处理,可以使用SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常:

首先需要在springmvc的配置文件中进行配置:

 <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <property name="exceptionAttribute" value="ex"></property>        <property name="exceptionMappings">            <props>                <prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>            </props>        </property> </bean>

这里如果发生数组越界异常就会跳转到错误页面

对应的控制器(用于制造异常尴尬

 @RequestMapping("/testSimpleMapingExceptionResolver")    public String testSimpleMapingExceptionResolver(@RequestParam("i") int i){        String[] values=new String[10];        System.out.println(values[i]);        return "success";    }
由于对错误取了别名,所以对应的错误页也要进行改写

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page isELIgnored="false" %><html><head>    <title>Title</title></head><body>错误页面!<br>${ex}</body></html>


0 0