<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-49  文章-56  trackbacks-0

    1 自定義異常類 SystemException.java

    public class SystemException extends RuntimeException{
     //自定義key
     private String key;
     //自定義參數
     private Object[] values;

     //實現父類的構造方法
     public SystemException() {
      super();
     }

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


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


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

     //自定義構造方法
     public SystemException(String message, String key) {
      super(message);
      this.key=key;
     }

     //自定義構造方法,帶一個參數
     public SystemException(String message, String key,Object value) {
      super(message);
      this.key=key;
      this.values=new Object[]{value};
     }
     
     //自定義構造方法,帶多個參數
     public SystemException(String message, String key,Object[] values) {
      super(message);
      this.key=key;
      this.values=values;
     }
     
     //相應的get方法
     public String getKey() {
      return key;
     }

     public Object[] getValues() {
      return values;
     }
    }
    2 自定義異常處理器 SystemExceptionHandler.java

    //作用:截獲SystemException,并根據SystemException中的信息動態創建ActionMessage等這些錯誤信息,
            將其存在request中
    public class SystemExceptionHandler extends ExceptionHandler{

     /**
      * 處理SystemException異常
      */
     @Override
     public ActionForward execute(Exception ex,//拋出的異常
             ExceptionConfig config,//struts-config.xml中的配置信息
             ActionMapping mapping,
              ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response) throws ServletException {
      
      ActionForward forward=null;
      //創建ActionForward
      if(config.getPath()!=null){
       //有path屬性,則根據path創建
       forward=new ActionForward(config.getPath());
      }else {
       //沒有path屬性,則根據input屬性創建
       forward=mapping.getInputForward();
      }
      if(ex instanceof SystemException){
       SystemException se=(SystemException)ex;
       //key可有可無,所以取出key進行判斷
       String key=se.getKey();
       ActionMessage error=null;
       //如果自定義的key為空,用struts的
       if(key==null){
        //拿出error.default和message,創建ActionMessage對象
        error=new ActionMessage(config.getKey(),se.getMessage());
       }else {
        //如果自定義的key有值
        if(se.getValues()!=null){
         error=new ActionMessage(key,se.getValues());
        }else {
         //如果自定義的key有值,則根據key創建ActionMessage對象
         error=new ActionMessage(key);
        }
       }
       //將這個ActionMessage放到request中。key為自定義的,error為ActionMessage對象
       //forward是要轉到什么地方,根據path屬性創建。"request"為scope的一個,也可以
       //用config.getScope()
       this.storeException(request, key, error, forward, config.getScope());
       return forward;
      }
      return super.execute(ex, config, mapping, form, request, response);
     }
    }


    3 編寫配置文件 struts-config.xml

    <global-exceptions>
       <exception key="error.default"
            type="java.lang.Exception"
            scope="request"
            path="/common/exception.jsp"
        <!-- 自定義的異常處理類 -->
            handler="org.oa.common.SystemExceptionHandler"/>
    </global-exceptions>

    4 編寫資源文件 ApplicationResources.properties

    error.default={0}
    error.org.del=Can't Del Orgnation,id is {0}!

    5 業務代碼

    throw new org.oa.common.SystemException("存在子機構,不允許刪除!","error.org.del",org.getOname());

    posted on 2009-11-30 08:17 junly 閱讀(508) 評論(0)  編輯  收藏 所屬分類: struts2/struts1.3/JSF
    主站蜘蛛池模板: 亚洲AⅤ视频一区二区三区| 免费无码专区毛片高潮喷水| 国产亚洲精品精品国产亚洲综合| AV无码免费永久在线观看| 国产免费福利体检区久久| 亚洲国产精品无码中文lv| 亚洲美女视频网站| 中文字幕不卡亚洲| 四虎免费久久影院| 毛片a级毛片免费观看免下载| 99re免费99re在线视频手机版| 成人一区二区免费视频| 国产成人亚洲精品无码AV大片| 精品亚洲成在人线AV无码| 久久久久久久亚洲Av无码| 国产亚洲精品美女久久久| 亚洲一本大道无码av天堂| 国产精品公开免费视频| 成人au免费视频影院| 美女视频黄免费亚洲| 57pao国产成视频免费播放| 久9这里精品免费视频| 免费人成网站在线观看不卡| 国产精品午夜免费观看网站| 免费大片av手机看片| 国产精品亚洲综合| 18禁亚洲深夜福利人口| 亚洲国产欧美一区二区三区| 午夜在线a亚洲v天堂网2019| 亚洲av无码一区二区三区观看| 亚洲韩国在线一卡二卡| 中文字幕亚洲精品| 亚洲特级aaaaaa毛片| 亚洲毛片基地日韩毛片基地| 亚洲码在线中文在线观看| 亚洲人成网站在线观看播放动漫| 亚洲首页在线观看| 亚洲AV无码乱码在线观看代蜜桃| 亚洲国产美女精品久久| 亚洲婷婷第一狠人综合精品| 亚洲国产成人无码AV在线 |