Struts提供了一個更簡單的方式來處理未捕獲的異常,并將用戶重定向到一個專門的錯誤頁面。您可以輕松地Struts配置到不同的異常有不同的錯誤頁面。
Struts的異常處理所使用的“exception”攔截容易。“exception”攔截器作為默認的棧的一部分,所以不必做任何額外的配置。它可為準備使用的盒。讓我們看到了一個簡單的Hello World示例進行一些修改在HelloWorldAction.java文件。在這里,我們特意推出了一個空指針異常在我們HelloWorldAction動作代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.yiibai.struts2; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{ private String name; public String execute(){ String x = null ; x = x.substring( 0 ); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this .name = name; } } |
讓我們 helloWorld.jsp保持內容如下:
1
2
3
4
5
6
7
8
9
10
|
<%@ page contentType= "text/html; charset=UTF-8" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Hello World, <s:property value= "name" /> </body> </html> |
以下是內容index.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language= "java" contentType= "text/html; charset=ISO-8859-1" pageEncoding= "ISO-8859-1" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action= "hello" > <label for = "name" >Please enter your name</label><br/> <input type= "text" name= "name" /> <input type= "submit" value= "Say Hello" /> </form> </body> </html> |
struts.xml 應該像這樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < constant name = "struts.devMode" value = "true" /> < package name = "helloworld" extends = "struts-default" > < action name = "hello" class = "com.yiibai.struts2.HelloWorldAction" method = "execute" > < result name = "success" >/HelloWorld.jsp</ result > </ action > </ package > </ struts > |
現在右擊項目名稱,并單擊Export > WAR File創建一個WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動Tomcat 服務器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:
輸入一個值“Struts2”,并提交頁面。應該看到以下頁面:
在上面的例子所示,默認的異常攔截器做了非常出色的處理異常。現在,讓我們創建一個專用的錯誤頁面,我們的例外。創建一個文件名為error.jsp 如以下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<%@ page language= "java" contentType= "text/html; charset=ISO-8859-1" pageEncoding= "ISO-8859-1" %> <%@ taglib prefix= "s" uri= "/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <title></title> </head> <body> This is my custom error page </body> </html> |
Let us now configure Struts to use this this error page in case of an exception. Let us modify thestruts.xml as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < constant name = "struts.devMode" value = "true" /> < package name = "helloworld" extends = "struts-default" > < action name = "hello" class = "com.yiibai.struts2.HelloWorldAction" method = "execute" > < exception-mapping exception = "java.lang.NullPointerException" result = "error" /> < result name = "success" >/HelloWorld.jsp</ result > < result name = "error" >/Error.jsp</ result > </ action > </ package >< br ></ struts > |
在上面的例子所示,現在我們已經配置 Struts使用專用error.jsp的NullPointerException異常。如果現在重新運行該程序,現在看到下面的輸出:
根據<exception-mapping…../>元素出現位置的不同,異常映射又可分為兩種:
局部異常映射:將<exception-mapping… />元素作為<action…/>元素的子元素配置;
全局異常映射:將<exception-mapping… />元素作為<global-exception-mappings… />元素的子元素配置;
全局異常映射對所有的Action都有效,但局部異常映射僅對該異常映射所在的Action有效。
如果局部異常映射和全局異常映射配置了同一個異常類型,在<action…./>元素內的局部異常映射將覆蓋全局異常映射。
Struts.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< 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 > |
Action
1
2
3
4
5
6
7
|
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
25
26
27
|
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
|
<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> |
在全局配置中的異常處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<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結果頁面,如果找到了,則直接跳轉到錯誤結果頁面,不管全局有沒有相同的結果,都被局部所覆蓋,如果在局部區域沒找到,則去全局區域找。