posts - 68, comments - 19, trackbacks - 0, articles - 1

          Struts1自動(dòng)異常處理

          Posted on 2011-02-25 21:05 viery 閱讀(551) 評(píng)論(0)  編輯  收藏
          1.異常類

          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,String message) {
              super(message);
              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;
            }

           

          }

          2.處理器

          public class SystemExceptionHandler extends ExceptionHandler {

           /**
            * 處理SystenException異常
            */
           @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,轉(zhuǎn)到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);

           }

          }

          3.struts配置
           <global-exceptions>
            <exception key="errors.detail"
                 type="com.v.oa.common.SystemException"
                 path="/common/exception.jsp"
                 scope="request"
                 handler="com.v.oa.common.SystemExceptionHandler">
                
            </exception>
           </global-exceptions>
          4.<html:errors/>
          5.國(guó)際化資源文件 可以用0001編碼 類似oracle異常編碼

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 通辽市| 固原市| 拉萨市| 龙井市| 邳州市| 兴安县| 永吉县| 玉林市| 龙里县| 延长县| 广元市| 灵丘县| 中阳县| 启东市| 华池县| 皋兰县| 亚东县| 平邑县| 白山市| 新疆| 江都市| 依安县| 新平| 贺州市| 定兴县| 岚皋县| 丰县| 织金县| 原平市| 龙口市| 通海县| 巴彦淖尔市| 万全县| 内黄县| 聂荣县| 云南省| 马山县| 台山市| 东兰县| 汤阴县| 嘉善县|