瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          struts中利用ExceptionHandler處理異常

          Posted on 2007-11-22 13:33 瘋狂 閱讀(3865) 評論(5)  編輯  收藏

                  在Struts1.X的版本中加入了對異常的處理,Exception Handling,有了它就不需要我們用try/catch等捕獲異常,一旦出現了我們已經定義的異常那么就會轉到相應得頁面,并且攜帶定制的信息。STRUTS框架提供了默認的異常處理org.apache.struts.action.ExceptionHandler,他的execute()方法負責處理異常。在需要實現自定義處理時重寫方法,可以在配置文件定義由誰來處理Action類中擲出的某種異常。
                 Struts框架處理異常的流程
                 struts的控制器負責捕獲各種異常,包括控制器運行中本身拋出的異常,以及調用模型的業務方法時拋出的異常。當struts的控制器捕獲到
          異常后,在異常處理代碼塊中,創建描述信息的actionmessage對象把它保存在acionmessages(或其子類actionerrors)對象中,然后把ACTIONMESSAGES保存在特定范圍(配置文件中的scope)。然后可以用<html:errors>檢索特定范圍內的actionmessages對象。

          下面是一個全局的處理類的例子:
                 
            首先介紹<global-exception>元素: 
           <global-exceptions>//元素可以包含一個或者多個<exception>元素
            <exception 
             className=""http://指定和元素對應的配置類,默認的不用動
             bundle=""http://Resource Bundle
             key="..." // <message-resources parameter="MessageResources" />中的key
             type="java.lang.Exception"http://指定需要處理異常類的名字,必須
             handler="com.myHander.SystemExceptionHandler"指定異常處理類默認是ExceptionHandler,
             path="/pub_exception.jsp"http://指定當前異常發生的時候轉發的路徑
             scope="request"http://指定ActionMessages實例存放的范圍
                  ......
            >

          </exception>
           </global-exceptions>

           
           
           
          2,定義自己的異常類:SystemException

          package com.ex;

          public class SystemException extends RuntimeException {

           private String key;//得到本地資源文件key
           private Object[] values;
           
           public SystemException() {
            super();
           }

           public SystemException(String message, Throwable arg1) {
            super(message, arg1);
           }

           public SystemException(String message) {
            super(message);
           }

           public SystemException(Throwable message) {
            super(message);
           }

           public SystemException(String key,String message, Throwable arg1) {
            super(message, arg1);
            this.key = key;
           }

           public SystemException(String key,Object value) {
            super();
            this.key = key;
            values = new Object[]{value};
           }
           
           public SystemException(String key,Object value1,Object value2) {
            super();
            this.key = key;
            values = new Object[]{value1,value2};
           }
           
           public SystemException(String key,Object[] values) {
            super();
            this.key = key;
            this.values = values;
           }

           public String getKey() {
            return key;
           }

           public Object[] getValues() {
            return values;
           }

          }

          定義自己的異常類處理類:SystemExceptionHandler,extendExceptionHandler,覆蓋execute方法,處理自己的異常類,其他的異常有父類自己處理


          package com.ex;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.apache.struts.Globals;
          import org.apache.struts.action.ActionForm;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.action.ActionMessage;
          import org.apache.struts.action.ExceptionHandler;
          import org.apache.struts.config.ExceptionConfig;

          import com.ex.SystemException;

          public class SystemExceptionHandler extends ExceptionHandler {

           @Override
           public ActionForward execute(Exception ex, ExceptionConfig ae,
             ActionMapping mapping, ActionForm formInstance,
             HttpServletRequest request, HttpServletResponse response)
             throws ServletException {
            
                  ActionForward forward = null;
                  ActionMessage error = null;
                  String property = null;  
            
                  if (ae.getPath() != null) {//配置文件中的path
                      forward = new ActionForward(ae.getPath());
                  } else {
                      forward = mapping.getInputForward();//
          如果沒有找到path,轉到input配置的路徑
                  }  
            
                  this.logException(ex);
                  
            
          //處理自定義的異常類SystemException
            if(ex instanceof SystemException){
             SystemException se = (SystemException)ex;
             //如果只有message,而沒有key
             if(se.getKey() == null){
              error = new ActionMessage(ae.getKey(), ex.getMessage());
              property = ae.getKey();
             }else{ //SystemException中有key值

              error = new ActionMessage(se.getKey(),se.getValues());
              property = se.getKey();
             }
                   request.setAttribute(Globals.EXCEPTION_KEY, ex);
                   this.storeException(request, property, error, forward, ae.getScope());
                  
                   return forward;
            }
            
            return super.execute(ex, ae, mapping, formInstance, request, response);
           }
           
          }
          struts-config.xml中配置:

              <global-exceptions>
            <exception
             type="java.lang.Exception"
             handler="com.ex.SystemExceptionHandler"
             path="/pub_exception.jsp"
             scope="request"
            >
            </exception>
           </global-exceptions>

          3,pub_exception.jsp頁面;
            
          獲得異常:<html:errors/>//當然:引入<%@ taglib prefix="html"  uri="http://struts.apache.org/tags-html" %>
          4,測試:TestHander(業務邏輯類)

          package com.ex;

          import com.ex.SystemException;

          public class TestHander {

              public void test(int t){
               
               if(t>2){
                
          throw new SystemException("不能大于2!");
                          //
          或者throw new SystemException("testkey",t);調用SystemException中的SystemException(String key,Object value) 構成方法得到資源文件定義的信息,當然 在MessageResources.properties文件里加上testkey= {0} con't greater than 2!當然還要加上  <message-resources parameter="MessageResources" />
                          
               }
               
              }
          }
          5,Action類:調用TestHander的test方法:
                (主要代碼) new TestHander().test(3);
          6,jsp調用acyion頁面頁面:
            <a href="testhander.do">異常</a>
          一點擊就會轉向pub_exception.jsp頁面顯示:不能大于2!

           


          評論

          # re: struts中利用ExceptionHandler處理異常[未登錄]  回復  更多評論   

          2007-11-22 17:54 by xiaoxiao
          up,正在學

          # re: struts中利用ExceptionHandler處理異常  回復  更多評論   

          2008-04-09 10:29 by binggo
          在配置文件中exception 不指定key好像是不可以的。
          如果不可以的話public SystemException(String message) 這個構造函數也沒有很么意義。

          # re: struts中利用ExceptionHandler處理異常  回復  更多評論   

          2008-06-14 11:39 by 浪子回頭
          請不要亂貼!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

          # re: struts中利用ExceptionHandler處理異常  回復  更多評論   

          2008-12-09 14:44 by s
          s

          # re: struts中利用ExceptionHandler處理異常  回復  更多評論   

          2009-01-05 22:23 by anms
          <global-exceptions>
          <exception key="some.key"
          type="com.framework.exception.BaseException"
          handler="com.ramework.struts.action.ExceptionHandlerAction" />
          </global-exceptions>

          我看到的一個異常處理 沒有Path=“uir...”那么它是怎樣轉到一個頁面上的呀

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 宾阳县| 楚雄市| 临桂县| 临夏市| 金阳县| 巴南区| 莲花县| 阿合奇县| 明溪县| 内黄县| 瓮安县| 延吉市| 新龙县| 旬阳县| 绵阳市| 布拖县| 德州市| 南川市| 宜兴市| 浮山县| 永善县| 富顺县| 南靖县| 友谊县| 贡嘎县| 台山市| 嫩江县| 泾川县| 祁东县| 沧州市| 重庆市| 霍林郭勒市| 滁州市| 永昌县| 昌图县| 泰兴市| 龙海市| 湖口县| 繁昌县| 祁门县| 开化县|