在xwork中,package的定義中增加了global-exception-mappings,action的定義中增加了exception-mapping,要想使用這2個特性,必須使用xwork-1.1.dtd才行.
DTD中,global-exception-mappings是這樣定義的,
<ELEMENT global-exception-mappings (exception-mapping+)>
也就是說如果要使用,至少包含一個異常映射.
exception-mapping的定義如下:
<!ELEMENT exception-mapping (#PCDATA|param)*>
<!ATTLIST exception-mapping
name CDATA #IMPLIED
exception CDATA #REQUIRED
result CDATA #REQUIRED
>

可以看到有3個屬性:名稱,異常類型,和結果.
經過一番查看,發現xwork-default.xml中還沒有定義相關的攔截器,搜索了一下,發現了ExceptionMappingInterceptor,看來它是把異常翻譯為相應的結果的.
(在最后發行版本中,應該會定義的.目前只好我們自己定義了.)
首先我們虛擬一個程序,假設程序會拋出幾種程序
Action定義如下:
package com.jscud.ww2test.exceptionmap; import com.opensymphony.xwork.ActionSupport; /** * @author scud http://www.jscud.com * */ public class ExpMappingAction extends ActionSupport { private int type; public String execute() throws NoRightException,NotFoundException { if(type==1) { throw new NoRightException(); } else if(type ==99) { throw new NotFoundException(); } else if(type==60) //其他異常 { throw new IllegalArgumentException(); } return SUCCESS; } public int getType() { return type; } public void setType(int type) { this.type = type; } } |
從Action可以看出,程序至少會拋出3種異常,我們如果使用異常映射,就要考慮到拋出的所有異常.
程序中用到的NoRightException,NotFoundException是為了演示而建立的2個簡單異常,無任何特殊代碼,繼承自Exception.
我們的Xwork.xml中的定義如下:
<package name="exceptionmapping" extends="webwork-default" namespace="/exceptionmap">
<interceptors>
<interceptor name="exceptionmapping" class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
<interceptor-stack name="myDefaultStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="exceptionmapping"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myDefaultStack"/>
<global-results>
<result name="noright" type="dispatcher">
<param name="location">/exceptionmap/noright.jsp</param>
</result>
<result name="exception" type="dispatcher">
<param name="location">/exceptionmap/exception.jsp</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping name="noright" exception="com.jscud.ww2test.exceptionmap.NoRightException" result="noright"/>
<exception-mapping name="exception" exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<action name="index" class="com.jscud.ww2test.exceptionmap.ExpMappingAction">
<exception-mapping name="notfound" exception="com.jscud.ww2test.exceptionmap.NotFoundException" result="notfound"/>
<result name="success" type="dispatcher">
<param name="location">/exceptionmap/index.jsp</param>
</result>
<result name="notfound" type="dispatcher">
<param name="location">/exceptionmap/notfound.jsp</param>
</result>
</action>
</package>
首先定義了一個exceptionmapping攔截器,用來指向ExceptionMappingInterceptor.
然后定義一個攔截器Stack,包含defaultStack和exceptionmapping,注意,exceptionmapping是在Stack的最后面,否則會發生不可預期的結果.
可以看到global-exception-mappings段包含了2個異常映射,一個為NoRight的處理,另外一個為對應java.lang.Exception的映射.
在Action的定義中包含了一個exception-mapping,對應NotFound異常.
沒有看到IllegalArgumentException的對應結果?? 攔截器對沒有定義的異常會依次找這個異常的父類,一級一級向上查找,例如IllegalArgumentException的最終父節點是Exception,就會轉向到Exception對應的結果. 如果一個異常有多個層次關系上的父類,那個關系最近就找誰.
演示頁面如下:

<%
@ page contentType="text/html; charset=GBK" %>

<%
@ taglib uri="webwork" prefix="ww" %>
<html>
<head>
<title>exception mapping</title>
</head>
<body>
<br>
Exception 1:
<a href="index.jspa?type=1">Exception 1</a>
<br><br>
Exception 99:
<a href="index.jspa?type=99">Exception 99</a>
<br><br>
Other Exception:
<a href="index.jspa?type=60">Exception 60</a>
<br><br>
Normal:
<a href="index.jspa">No Exception</a>
<br><br>
</body>
</html>
notfound.jsp,exception.jsp,noright.jsp是三個最簡單的jsp頁面,自己編寫即可.
運行程序,發現根據異常類型會轉向相應的異常頁面.
如果你自己的程序中的異常沒有在異常映射中得到對應的result,程序會拋出異常,所以應該定義一個Exception的對應result.
除經特別注明外,本文章版權歸JScud Develop團隊或其原作者所有.
轉載請注明作者和來源. scud(飛云小俠) 歡迎訪問 JScud Develop