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

          Struts1自動異常處理

          Posted on 2011-02-25 21:05 viery 閱讀(551) 評論(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,轉到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.國際化資源文件 可以用0001編碼 類似oracle異常編碼

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


          網站導航:
           
          主站蜘蛛池模板: 汤阴县| 鄱阳县| 南开区| 大丰市| 二手房| 金昌市| 潼南县| 定襄县| 海南省| 米泉市| 常州市| 漳浦县| 瓮安县| 甘谷县| 宝坻区| 长子县| 宣化县| 顺平县| 宣武区| 民丰县| 精河县| 石首市| 平顶山市| 郧西县| 嘉荫县| 聂拉木县| 千阳县| 周口市| 车致| 从化市| 昌黎县| 龙川县| 嘉义县| 金堂县| 平乡县| 娄烦县| 乐陵市| 旺苍县| 鞍山市| 开鲁县| 延寿县|