<!ATTLIST exception-mapping
name CDATA #IMPLIED
exception CDATA #REQUIRED
result CDATA #REQUIRED
>
經過一番查看,發現xwork-default.xml中還沒有定義相關的攔截器,搜索了一下,發現了ExceptionMappingInterceptor,看來它是把異常翻譯爲相應的結果的.
(在最後發行版本中,應該會定義的.目前只好我們自己定義了.)
import com.opensymphony.xwork.ActionSupport;
/**
* @author scud [url]http://www.jscud.com[/url]
*
*/
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;
}
}
<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>
然後定義一個攔截器Stack,包含defaultStack和exceptionmapping,注意,exceptionmapping是在Stack的最後面,否則會發生不可預期的結果.
<%@ 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>
pageEncoding="GB18030"%>
<%@ taglib prefix="ww" uri="/webwork"%>
<%@ page import="com.opensymphony.xwork.util.OgnlValueStack"%>
<%@ page import="com.opensymphony.xwork.ActionContext"%>
<%@ page import="com.opensymphony.xwork.interceptor.ExceptionHolder"%>
<%@ page import="com.mdcl.framework.exception.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.mdcl.timecard.util.Translate"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
異常信息
<%
String tipMessge = null;//中文提示信息
OgnlValueStack s = ActionContext.getContext().getValueStack();
ExceptionHolder e;
String s1 = new String("");;
String name = new String("");
for(int i = s.size();i>0;i--){
Object obj = s.pop();
if(obj instanceof ExceptionHolder){
e = (ExceptionHolder)obj;
Object o = e.getException();
if(o instanceof AddException){
AddException we = (AddException)o;
tipMessge = Translate.translate("ErrorException");
}else if(o instanceof DaoException){
DaoException we = (DaoException)o;
tipMessge = Translate.translate("ErrorException");
}
else if(o instanceof FindException){
FindException we = (FindException)o;
tipMessge = Translate.translate("ErrorException");
}
s1 =e.getExceptionStack();
System.out.println(tipMessge);
break;
}
}
OutputStream b = response.getOutputStream();
%>
<br/>
<%=tipMessge%>
<%=s1%>
</body>
</html>