天天看點

spring MVC中定義異常頁面

下面看我曾經的一個項目的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>&lt;?</code><code>xml</code> <code>version="1.0" encoding="UTF-8" ?&gt;</code>

<code>&lt;</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"&gt;</code>

<code>    </code><code>&lt;!-- 掃描web包,應用Spring的注解 --&gt;</code>

<code>    </code><code>&lt;</code><code>context:component-scan</code> <code>base-package="com.xxx.training.spring.mvc"/&gt;</code>

<code>    </code><code>&lt;!-- 配置視圖解析器,将ModelAndView及字元串解析為具體的頁面 --&gt;</code>

<code>    </code><code>&lt;</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"/&gt;</code>

<code>    </code><code>&lt;!--定義異常處理頁面--&gt;</code>

<code>    </code><code>&lt;</code><code>bean</code> <code>id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"&gt;</code>

<code>        </code><code>&lt;</code><code>property</code> <code>name="exceptionMappings"&gt;</code>

<code>            </code><code>&lt;</code><code>props</code><code>&gt;</code>

<code>                </code><code>&lt;</code><code>prop</code> <code>key="java.sql.SQLException"&gt;outException&lt;/</code><code>prop</code><code>&gt;</code>

<code>                </code><code>&lt;</code><code>prop</code> <code>key="java.io.IOException"&gt;outException&lt;/</code><code>prop</code><code>&gt;</code>

<code>            </code><code>&lt;/</code><code>props</code><code>&gt;</code>

<code>        </code><code>&lt;/</code><code>property</code><code>&gt;</code>

<code>    </code><code>&lt;/</code><code>bean</code><code>&gt;</code>

<code>&lt;/</code><code>beans</code><code>&gt;</code>

  上面的定義異常處理部分的解釋為:隻要發生了SQLException或者IOException異常,就會自動跳轉到WEB-INF/views/outException.jsp頁面。

一般情況下我們的outException.jsp頁面的代碼為:

<code>&lt;%@ page contentType=</code><code>"text/html;charset=UTF-8"</code> <code>language=</code><code>"java"</code> <code>%&gt;</code>

<code>&lt;html&gt;</code>

<code>&lt;head&gt;</code>

<code>    </code><code>&lt;title&gt;異常處理頁面&lt;/title&gt;</code>

<code>&lt;/head&gt;</code>

<code>&lt;body&gt;</code>

<code>&lt;% Exception ex = (Exception) request.getAttribute(</code><code>"Exception"</code><code>);%&gt;</code>

<code>&lt;H2&gt;Exception:&lt;%=ex.getMessage()%&gt;</code>

<code>&lt;/H2&gt;</code>

<code>&lt;/body&gt;</code>

<code>&lt;/html&gt;</code>

  當然你也可以修改樣式,這個就看個人喜好了、

另外記得要在web.xml也使用類似下面的方式處理異常哦。:

<code>&lt;</code><code>error-page</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>error-code</code><code>&gt;404&lt;/</code><code>error-code</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>location</code><code>&gt;/WEB-INF/pages/404.jsp&lt;/</code><code>location</code><code>&gt;</code>

<code> </code><code>&lt;/</code><code>error-page</code><code>&gt;</code>

<code> </code><code>&lt;</code><code>error-page</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>exception-type</code><code>&gt;java.lang.Exception&lt;/</code><code>exception-type</code><code>&gt;</code>

<code>     </code><code>&lt;</code><code>location</code><code>&gt;/WEB-INF/pages/exception.jsp&lt;/</code><code>location</code><code>&gt;</code>

  因為這兩個異常處理的次元是不一樣的,簡單說,spring的resolver是spring内部使用的,而web。xml裡的是整個webapp共同使用的。

建議兩個都配置上,

因為spring的resolver可以和spring結合的更緊密,可擴充的更多。

==============================================================================

本文轉自被遺忘的部落格園部落格,原文連結:http://www.cnblogs.com/rollenholt/archive/2012/12/25/2832731.html,如需轉載請自行聯系原作者