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

可以看到有3個(gè)屬性:名稱,異常類型,和結(jié)果.
經(jīng)過一番查看,發(fā)現(xiàn)xwork-default.xml中還沒有定義相關(guān)的攔截器,搜索了一下,發(fā)現(xiàn)了ExceptionMappingInterceptor,看來它是把異常翻譯為相應(yīng)的結(jié)果的.
(在最后發(fā)行版本中,應(yīng)該會(huì)定義的.目前只好我們自己定義了.)
首先我們虛擬一個(gè)程序,假設(shè)程序會(huì)拋出幾種程序
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可以看出,程序至少會(huì)拋出3種異常,我們?nèi)绻褂卯惓S成?就要考慮到拋出的所有異常.
程序中用到的NoRightException,NotFoundException是為了演示而建立的2個(gè)簡(jiǎn)單異常,無任何特殊代碼,繼承自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>
首先定義了一個(gè)exceptionmapping攔截器,用來指向ExceptionMappingInterceptor.
然后定義一個(gè)攔截器Stack,包含defaultStack和exceptionmapping,注意,exceptionmapping是在Stack的最后面,否則會(huì)發(fā)生不可預(yù)期的結(jié)果.
可以看到global-exception-mappings段包含了2個(gè)異常映射,一個(gè)為NoRight的處理,另外一個(gè)為對(duì)應(yīng)java.lang.Exception的映射.
在Action的定義中包含了一個(gè)exception-mapping,對(duì)應(yīng)NotFound異常.
沒有看到IllegalArgumentException的對(duì)應(yīng)結(jié)果?? 攔截器對(duì)沒有定義的異常會(huì)依次找這個(gè)異常的父類,一級(jí)一級(jí)向上查找,例如IllegalArgumentException的最終父節(jié)點(diǎn)是Exception,就會(huì)轉(zhuǎn)向到Exception對(duì)應(yīng)的結(jié)果. 如果一個(gè)異常有多個(gè)層次關(guān)系上的父類,那個(gè)關(guān)系最近就找誰.
演示頁面如下:

<%
@ 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是三個(gè)最簡(jiǎn)單的jsp頁面,自己編寫即可.
運(yùn)行程序,發(fā)現(xiàn)根據(jù)異常類型會(huì)轉(zhuǎn)向相應(yīng)的異常頁面.
如果你自己的程序中的異常沒有在異常映射中得到對(duì)應(yīng)的result,程序會(huì)拋出異常,所以應(yīng)該定義一個(gè)Exception的對(duì)應(yīng)result.
除經(jīng)特別注明外,本文章版權(quán)歸JScud Develop團(tuán)隊(duì)或其原作者所有.
轉(zhuǎn)載請(qǐng)注明作者和來源. scud(飛云小俠) 歡迎訪問 JScud Develop