下面看我曾經的一個項目的spring配置檔案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<code><?</code><code>xml</code> <code>version="1.0" encoding="UTF-8" ?></code>
<code><</code><code>beans</code> <code>xmlns="http://www.springframework.org/schema/beans"</code>
<code> </code><code>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"</code>
<code> </code><code>xmlns:context="http://www.springframework.org/schema/context"</code>
<code> </code><code>xsi:schemaLocation="http://www.springframework.org/schema/beans</code>
<code> </code><code>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</code>
<code> </code><code>http://www.springframework.org/schema/context</code>
<code> </code><code>http://www.springframework.org/schema/context/spring-context-3.0.xsd"></code>
<code> </code><code><!-- 掃描web包,應用Spring的注解 --></code>
<code> </code><code><</code><code>context:component-scan</code> <code>base-package="com.xxx.training.spring.mvc"/></code>
<code> </code><code><!-- 配置視圖解析器,将ModelAndView及字元串解析為具體的頁面 --></code>
<code> </code><code><</code><code>bean</code>
<code> </code><code>class="org.springframework.web.servlet.view.InternalResourceViewResolver"</code>
<code> </code><code>p:viewClass="org.springframework.web.servlet.view.JstlView"</code>
<code> </code><code>p:prefix="/WEB-INF/views/"</code>
<code> </code><code>p:suffix=".jsp"/></code>
<code> </code><code><!--定義異常處理頁面--></code>
<code> </code><code><</code><code>bean</code> <code>id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"></code>
<code> </code><code><</code><code>property</code> <code>name="exceptionMappings"></code>
<code> </code><code><</code><code>props</code><code>></code>
<code> </code><code><</code><code>prop</code> <code>key="java.sql.SQLException">outException</</code><code>prop</code><code>></code>
<code> </code><code><</code><code>prop</code> <code>key="java.io.IOException">outException</</code><code>prop</code><code>></code>
<code> </code><code></</code><code>props</code><code>></code>
<code> </code><code></</code><code>property</code><code>></code>
<code> </code><code></</code><code>bean</code><code>></code>
<code></</code><code>beans</code><code>></code>
上面的定義異常處理部分的解釋為:隻要發生了SQLException或者IOException異常,就會自動跳轉到WEB-INF/views/outException.jsp頁面。
一般情況下我們的outException.jsp頁面的代碼為:
<code><%@ page contentType=</code><code>"text/html;charset=UTF-8"</code> <code>language=</code><code>"java"</code> <code>%></code>
<code><html></code>
<code><head></code>
<code> </code><code><title>異常處理頁面</title></code>
<code></head></code>
<code><body></code>
<code><% Exception ex = (Exception) request.getAttribute(</code><code>"Exception"</code><code>);%></code>
<code><H2>Exception:<%=ex.getMessage()%></code>
<code></H2></code>
<code></body></code>
<code></html></code>
當然你也可以修改樣式,這個就看個人喜好了、
另外記得要在web.xml也使用類似下面的方式處理異常哦。:
<code><</code><code>error-page</code><code>></code>
<code> </code><code><</code><code>error-code</code><code>>404</</code><code>error-code</code><code>></code>
<code> </code><code><</code><code>location</code><code>>/WEB-INF/pages/404.jsp</</code><code>location</code><code>></code>
<code> </code><code></</code><code>error-page</code><code>></code>
<code> </code><code><</code><code>error-page</code><code>></code>
<code> </code><code><</code><code>exception-type</code><code>>java.lang.Exception</</code><code>exception-type</code><code>></code>
<code> </code><code><</code><code>location</code><code>>/WEB-INF/pages/exception.jsp</</code><code>location</code><code>></code>
因為這兩個異常處理的次元是不一樣的,簡單說,spring的resolver是spring内部使用的,而web。xml裡的是整個webapp共同使用的。
建議兩個都配置上,
因為spring的resolver可以和spring結合的更緊密,可擴充的更多。
==============================================================================
本文轉自被遺忘的部落格園部落格,原文連結:http://www.cnblogs.com/rollenholt/archive/2012/12/25/2832731.html,如需轉載請自行聯系原作者