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

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

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

    隨筆-124  評(píng)論-49  文章-56  trackbacks-0

    1 自定義異常類 SystemException.java

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

     //實(shí)現(xiàn)父類的構(gòu)造方法
     public SystemException() {
      super();
     }

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


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


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

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

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

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

    //作用:截獲SystemException,并根據(jù)SystemException中的信息動(dòng)態(tài)創(chuàng)建ActionMessage等這些錯(cuò)誤信息,
            將其存在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;
      //創(chuàng)建ActionForward
      if(config.getPath()!=null){
       //有path屬性,則根據(jù)path創(chuàng)建
       forward=new ActionForward(config.getPath());
      }else {
       //沒(méi)有path屬性,則根據(jù)input屬性創(chuàng)建
       forward=mapping.getInputForward();
      }
      if(ex instanceof SystemException){
       SystemException se=(SystemException)ex;
       //key可有可無(wú),所以取出key進(jìn)行判斷
       String key=se.getKey();
       ActionMessage error=null;
       //如果自定義的key為空,用struts的
       if(key==null){
        //拿出error.default和message,創(chuàng)建ActionMessage對(duì)象
        error=new ActionMessage(config.getKey(),se.getMessage());
       }else {
        //如果自定義的key有值
        if(se.getValues()!=null){
         error=new ActionMessage(key,se.getValues());
        }else {
         //如果自定義的key有值,則根據(jù)key創(chuàng)建ActionMessage對(duì)象
         error=new ActionMessage(key);
        }
       }
       //將這個(gè)ActionMessage放到request中。key為自定義的,error為ActionMessage對(duì)象
       //forward是要轉(zhuǎn)到什么地方,根據(jù)path屬性創(chuàng)建。"request"為scope的一個(gè),也可以
       //用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 業(yè)務(wù)代碼

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

    posted on 2009-11-30 08:17 junly 閱讀(500) 評(píng)論(0)  編輯  收藏 所屬分類: struts2/struts1.3/JSF
    主站蜘蛛池模板: 亚洲精品无码久久久久sm| 久久久久久国产精品免费免费男同| 久久夜色精品国产噜噜亚洲a| 亚洲精品中文字幕无码AV| 久久久久亚洲Av无码专| 亚洲精品免费视频| 99人中文字幕亚洲区| 亚洲尹人九九大色香蕉网站| 午夜亚洲www湿好大| 亚洲免费视频在线观看| 久久久久亚洲AV无码观看| 91亚洲一区二区在线观看不卡| 亚洲综合婷婷久久| 亚洲综合无码一区二区三区| 亚洲国产日韩在线成人蜜芽| 亚洲天堂电影在线观看| 久久亚洲精品国产精品婷婷| 亚洲AV性色在线观看| 色老头综合免费视频| 三级黄色免费观看| 午夜视频免费在线观看| 国产免费一区二区三区| 妞干网免费观看视频| 国产一区二区三区在线免费| 亚洲日本一区二区一本一道| 国产亚洲一区二区精品| 老汉色老汉首页a亚洲| 97久久国产亚洲精品超碰热| 校园亚洲春色另类小说合集 | 亚洲成a人一区二区三区| 久久久久亚洲精品中文字幕| 久久精品国产亚洲AV果冻传媒| 亚洲欧洲国产精品久久| 最新亚洲春色Av无码专区| 无套内谢孕妇毛片免费看看| 国产真人无码作爱免费视频 | 亚洲国产aⅴ成人精品无吗| 免费在线观看自拍性爱视频| 最近2019中文免费字幕在线观看 | 本道天堂成在人线av无码免费| 中文字幕日本人妻久久久免费|