因為在Action的execute方法聲明時就拋出了Exception異常,所以我們無需再execute方法中捕捉異常,僅需在struts.xml 中配置異常處理。
為了使用Struts2的異常處理機制,必須打開Struts2的異常映射功能,這需要exception攔截器。在struts-default.xml文件中已經開啟了exception攔截器。
聲明式異常捕捉
Struts2的異常處理機制是通過在struts.xml文件中配置<exception-mapping……/>元素完成的,配置該元素時,需要指定兩個屬性:
exception:指定該異常類型的完全限定名。
result:指定邏輯視圖名。
根據<exception-mapping…../>元素出現位置的不同,異常映射又可分為兩種:
局部異常映射:將<exception-mapping… />元素作為<action…/>元素的子元素配置;
全局異常映射:將<exception-mapping… />元素作為<global-exception-mappings… />元素的子元素配置;
全局異常映射對所有的Action都有效,但局部異常映射僅對該異常映射所在的Action有效。
如果局部異常映射和全局異常映射配置了同一個異常類型,在<action…./>元素內的局部異常映射將覆蓋全局異常映射。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Struts.xml < package name = "ssh2" extends = "struts-default" > < global-results > < result name = "sql" >/exception.jsp</ result > < result name = "root" >/exception.jsp</ result > </ global-results > < global-exception-mappings > < exception-mapping exception = "java.sql.SQLException" result = "sql" /> < exception-mapping exception = "java.lang.Exception" result = "root" /> </ global-exception-mappings > < action name = "login" class = "loginAction" > < result >/welcome.jsp</ result > < result name = "nullPointer" >/nullPointer.jsp</ result > < exception-mapping exception = "java.lang.NullPointerException" result = "nullPointer" /> </ action > </ package > |
1
2
3
4
5
6
7
8
|
Action public class loginAction extends ActionSupport { public String add() throws SQLException { return "toadd" ; } } |
有異常往外拋即可。你也可以在方法里面拋,比如throw SQLException。
我們可以使用Struts2的標簽輸出異常信息:
輸出異常的message屬性信息:<s:property value="exception.message" />
輸出異常堆棧信息:<s:property value="exceptionStack" />。
有了處理系統異常的基礎,我們來看一看自定義異常:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.exception ; public class MyException extends Exception { private String message; public MyException(String message) { super (message); this .message = message ; } public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } } public String execute() throws Exception { if (! "hello" .equals(usename) || ! "world" .equals(password)) { throw new MyException( "用戶名或密碼錯誤,您發現了吧!" ); } return "success" ; } |
在action配置中的異常處理
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
|
< struts > < package name = "struts2" extends = "struts-default" > < action name = "login" class = "com.struts2.LoginAction" > < exception-mapping result = "myex" exception = "com.exception.MyException" > </ exception-mapping > < result name = "myex" >/error.jsp</ result > < result name = "success" >/result.jsp</ result > </ action > </ package > </ struts > 在全局配置中的異常處理 < struts > < package name = "struts2" extends = "struts-default" > < global-results > < result name = "myexception1" >/error.jsp</ result > </ global-results > < global-exception-mappings > < exception-mapping result = "myexception1" exception = "com.exception.MyException" > </ exception-mapping > </ global-exception-mappings > < action name = "login" class = "com.struts2.LoginAction" > < result name = "success" >/result.jsp</ result > </ action > </ package > </ struts > |
錯誤頁面error.jsp
1
2
3
4
5
6
7
8
|
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> < html > < body > <!-- 這個exception 是 exception="com.exception.MyException" --> < s:property value = "exception.message" /> </ body > </ html > |
總結
局部異常處理比全局異常處理高,并且可覆蓋全局異常處理,如果定義了全局異常映射,那么會對所有的Action生效,反之定義了局部異常映射則會對當前Action生效,
如果在全局區域和局部區域定義了相同的異常映射,首先去局部異常區域找result結果頁面,如果找到了,則直接跳轉到錯誤結果頁面,不管全局有沒有相同的結果,都被局部所覆蓋,如果在局部區域沒找到,則去全局區域找。
原文鏈接:http://blog.csdn.net/woshixuye/article/details/7738251#comments