ALL is Well!

          敏捷是一條很長(zhǎng)的路,摸索著前進(jìn)著

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            30 隨筆 :: 23 文章 :: 71 評(píng)論 :: 0 Trackbacks
          本文為原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明出處BlogJava

          Struts2 版本 2.2.1
          Freemarker版本 2.3.16

          此統(tǒng)一處理的目的在于 Web層、Service層拋出的業(yè)務(wù)異常以統(tǒng)一的格式顯示在頁面的固定位置。

          首先定義我們的業(yè)務(wù)異常類。

          public abstract class BaseException extends RuntimeException {
              
          private static final long serialVersionUID = -6765360320533958383L;

              
          private String messageCode;

              
          public String getMessageCode() {
                  
          return messageCode;
              }


              
          public void setMessageCode(String messageCode) {
                  
          this.messageCode = messageCode;
              }


              
          public BaseException() {
                  
          super();
              }


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


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


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


              
          public BaseException(String messageCode, String message) {
                  
          super(message);
                  setMessageCode(messageCode);

              }


              
          public BaseException(String messageCode, String message, Throwable cause) {
                  
          super(message, cause);
                  setMessageCode(messageCode);
              }


          public class BusinessException extends BaseException {

              
          private static final long serialVersionUID = -1657938434382769721L;
                             
              
          public BusinessException() {
                  
          super();
              }

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

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

              
              
          public BusinessException(String messageCode, String message) {
                  
          super(messageCode, message);
                  setMessageCode(messageCode);
              }

              
              
          public BusinessException(String messageCode, String message, Throwable cause) {
                  
          super(messageCode, message, cause);
                  setMessageCode(messageCode);
              }

          }



          攔截器類:ErrorHandlingInterceptor.java 用于攔截異常,并在此統(tǒng)一處理
          public class ErrorHandlingInterceptor extends AbstractInterceptor {

              
          private static final long serialVersionUID = 1L;

              @Override
              
          public String intercept(ActionInvocation invocation) throws Exception {
                  
          try {
                      
          return invocation.invoke();
                  }
           catch (Exception e) {
                      e.printStackTrace();
                      handleException(e);
                  }

                  
          return Action.INPUT;
              }

              
              
          /**
               * 處理異常
               * 
          @param e
               
          */

              
          private void handleException(Exception e) {
                  
          boolean handled = false;
                  Throwable throwEx 
          = e;
                  
          while (throwEx != null{
                      
          if(throwEx instanceof BusinessException) {
                          BusinessException be 
          = (BusinessException)throwEx;
                          String errorCode 
          = be.getMessageCode();
                          
                          
          // 從緩存中通過ErrorCode取得對(duì)應(yīng)message
                          
          // 實(shí)現(xiàn)略
                          String errorMsg = getMessage(errorCode);
                          
                          
          // 頁面顯示錯(cuò)誤提示信息
                          fillError4Display(errorMsg);
                          handled 
          = true;
                      }
           
                      throwEx 
          = throwEx.getCause();
                  }

                  
                  
          if(!handled) {
                      fillDefaultError();
                  }

              }

              
              
          private HttpServletRequest getRequest() {
                  
          return (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
              }

              
              
          private void fillDefaultError() {
                  fillError4Display(
          "系統(tǒng)忙,請(qǐng)稍候再試。");
              }

              
              
          private void fillError4Display(String msg) {
                  getRequest().setAttribute(
          "_error_msg_", msg);
              }

          }

          攔截所有的異常,并對(duì)其進(jìn)行處理。
          當(dāng)為 自定義的BusinessException時(shí),根據(jù)拋出異常時(shí)的msgCode,取得對(duì)應(yīng)的顯示信息。
          msgCode與顯示信息的對(duì)應(yīng)關(guān)系 可先配置好,系統(tǒng)啟動(dòng)時(shí)將其緩存起來。

          如果非BusinessException,則統(tǒng)一顯示為 “系統(tǒng)忙,請(qǐng)稍候再試。”

          將要顯示的信息設(shè)置到Request中,下面來看看Freemarker模板的寫法:

          msg.ftl

          <div id='_err_msg_div'>
              
          <#if Request['_error_msg_']?exists>
                  ${Request['_error_msg_']}
              
          </#if>
          </div>

          <script type="text/javascript">
          if (!this.Message) {
              
          this.Message = {};
              (
          function() {
                  
          /**
                   * show client message
                   
          */

                  Message.showMsg 
          = function(msg) {
                      document.getElementById(
          "_err_msg_div").innerHTML = msg;
                  }
          ;
              }
          )();
          }
          ;
          </script>

          在使用時(shí),只要在頁面上想要展現(xiàn)異常信息的地方插入如下代碼即可:
          <#include "/msg.ftl">

          這樣 系統(tǒng)中的異常 將會(huì)被統(tǒng)一的顯示。

          當(dāng)使用js做前臺(tái)的表單驗(yàn)證的時(shí)候,提示用戶的輸入有問題,則可以使用 Message.showMsg('...'),提示信息也會(huì)顯示在同一個(gè)位置。

          這樣就實(shí)現(xiàn)了異常提示信息的統(tǒng)一展示了。

          這是一個(gè)比較簡(jiǎn)易的實(shí)現(xiàn),只提供一個(gè)思路。

          posted on 2010-11-16 14:53 李 明 閱讀(2800) 評(píng)論(0)  編輯  收藏 所屬分類: Struts2Freemarker
          主站蜘蛛池模板: 平果县| 全南县| 岱山县| 青铜峡市| 岫岩| 梨树县| 大连市| 湾仔区| 二连浩特市| 富阳市| 象山县| 尉犁县| 福泉市| 垦利县| 甘德县| 雅安市| 崇文区| 中方县| 松江区| 永胜县| 抚顺县| 安阳县| 治多县| 上杭县| 泸定县| 宝应县| 林周县| 古交市| 象山县| 泌阳县| 舞钢市| 九龙县| 水城县| 合水县| 祥云县| 千阳县| 类乌齐县| 琼结县| 枝江市| 吉林市| 江安县|