Struts提供了一個(gè)更簡單的方式來處理未捕獲的異常,并將用戶重定向到一個(gè)專門的錯(cuò)誤頁面。您可以輕松地Struts配置到不同的異常有不同的錯(cuò)誤頁面。
Struts的異常處理所使用的“exception”攔截容易。“exception”攔截器作為默認(rèn)的棧的一部分,所以不必做任何額外的配置。它可為準(zhǔn)備使用的盒。讓我們看到了一個(gè)簡單的Hello World示例進(jìn)行一些修改在HelloWorldAction.java文件。在這里,我們特意推出了一個(gè)空指針異常在我們HelloWorldAction動(dòng)作代碼。
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保持內(nèi)容如下:
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> |
以下是內(nèi)容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 應(yīng)該像這樣:
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 > |
現(xiàn)在右擊項(xiàng)目名稱,并單擊Export > WAR File創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat 服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會(huì)給出以下畫面:
輸入一個(gè)值“Struts2”,并提交頁面。應(yīng)該看到以下頁面:
在上面的例子所示,默認(rèn)的異常攔截器做了非常出色的處理異常。現(xiàn)在,讓我們創(chuàng)建一個(gè)專用的錯(cuò)誤頁面,我們的例外。創(chuàng)建一個(gè)文件名為error.jsp 如以下內(nèi)容:
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 > |
在上面的例子所示,現(xiàn)在我們已經(jīng)配置 Struts使用專用error.jsp的NullPointerException異常。如果現(xiàn)在重新運(yùn)行該程序,現(xiàn)在看到下面的輸出:
根據(jù)<exception-mapping…../>元素出現(xiàn)位置的不同,異常映射又可分為兩種:
局部異常映射:將<exception-mapping… />元素作為<action…/>元素的子元素配置;
全局異常映射:將<exception-mapping… />元素作為<global-exception-mappings… />元素的子元素配置;
全局異常映射對所有的Action都有效,但局部異常映射僅對該異常映射所在的Action有效。
如果局部異常映射和全局異常映射配置了同一個(gè)異常類型,在<action…./>元素內(nèi)的局部異常映射將覆蓋全局異常映射。
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的標(biāo)簽輸出異常信息:
輸出異常的message屬性信息:<s:property value="exception.message" />
輸出異常堆棧信息:<s:property value="exceptionStack" />。
有了處理系統(tǒng)異常的基礎(chǔ),我們來看一看自定義異常:
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( "用戶名或密碼錯(cuò)誤,您發(fā)現(xiàn)了吧!" ); } 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> |
錯(cuò)誤頁面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> <!-- 這個(gè)exception 是 exception= "com.exception.MyException" --> <s:property value= "exception.message" /> </body> </html> |
總結(jié)
局部異常處理比全局異常處理高,并且可覆蓋全局異常處理,如果定義了全局異常映射,那么會(huì)對所有的Action生效,反之定義了局部異常映射則會(huì)對當(dāng)前Action生效,
如果在全局區(qū)域和局部區(qū)域定義了相同的異常映射,首先去局部異常區(qū)域找result結(jié)果頁面,如果找到了,則直接跳轉(zhuǎn)到錯(cuò)誤結(jié)果頁面,不管全局有沒有相同的結(jié)果,都被局部所覆蓋,如果在局部區(qū)域沒找到,則去全局區(qū)域找。