WebWork2.2新特性之異常映射/全局異常映射(有異常的頁面顯示)

 
 
 
在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 [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;
     }
}  
 從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>
jsp頁面的異常顯示:其中AddException,DaoException,FindException是自己定義的異常,繼承exception
<%@ page language="java" contentType="text/html; charset=GB18030"
    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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章