SpringMVC的异常处理,SimpleMappingExceptionResolver只能简单的处理异常
当发生异常的时候,根据发生的异常类型跳转到指定的页面来显示异常信息
首先我们需要顶一个异常的页面,用于serlvert异常后跳转
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title>异常处理页面</title>
</head>
<body>
<% Exception ex = (Exception) request.getAttribute(
"Exception"
);%>
<H2>Exception:<%=ex.getMessage()%>
</H2>
</body>
</html>
然后在xml中配置一个bean
<--使用SimpleMappingExceptionResolver来映射异常>
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>failure</value>
</property>
<property name="exceptionMappings">
<props>
<--异常页面-->
<prop key="java.lang.Exception">hasNoRight.jsp</prop>
</props>
</property>
</bean>
另外记得要在web.xml也使用类似下面的方式处理异常哦。:
<
error-page
>
<
error-code
>404</
error-code
>
<
location
>/WEB-INF/pages/404.jsp</
location
>
</
error-page
>
<
error-page
>
<
exception-type
>java.lang.Exception</
exception-type
>
<
location
>/WEB-INF/pages/exception.jsp</
location
>
</
error-page
>
因为这两个异常处理的维度是不一样的,简单说,spring的resolver是spring内部使用的,而web。xml里的是整个webapp共同使用的。