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

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

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

    ALL is Well!

    敏捷是一條很長的路,摸索著前進著

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks
    本文為原創,歡迎轉載,轉載請注明出處BlogJava

    Struts2 版本 2.2.1
    Freemarker版本 2.3.16

    此統一處理的目的在于 Web層、Service層拋出的業務異常以統一的格式顯示在頁面的固定位置。

    首先定義我們的業務異常類。

    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 用于攔截異常,并在此統一處理
    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取得對應message
                    
    // 實現略
                    String errorMsg = getMessage(errorCode);
                    
                    
    // 頁面顯示錯誤提示信息
                    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(
    "系統忙,請稍候再試。");
        }

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

    }

    攔截所有的異常,并對其進行處理。
    當為 自定義的BusinessException時,根據拋出異常時的msgCode,取得對應的顯示信息。
    msgCode與顯示信息的對應關系 可先配置好,系統啟動時將其緩存起來。

    如果非BusinessException,則統一顯示為 “系統忙,請稍候再試。”

    將要顯示的信息設置到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>

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

    這樣 系統中的異常 將會被統一的顯示。

    當使用js做前臺的表單驗證的時候,提示用戶的輸入有問題,則可以使用 Message.showMsg('...'),提示信息也會顯示在同一個位置。

    這樣就實現了異常提示信息的統一展示了。

    這是一個比較簡易的實現,只提供一個思路。

    posted on 2010-11-16 14:53 李 明 閱讀(2799) 評論(0)  編輯  收藏 所屬分類: Struts2Freemarker
    主站蜘蛛池模板: 九九精品成人免费国产片| 亚洲А∨精品天堂在线| 三年片在线观看免费西瓜视频| 亚洲一卡二卡三卡四卡无卡麻豆| 亚洲日韩v无码中文字幕| 国产又粗又长又硬免费视频| 日本片免费观看一区二区| 国产真人无码作爱视频免费| 免费一级特黄特色大片| 亚洲精品无码久久久久YW| 亚洲小视频在线播放| 亚洲国产综合专区在线电影| 亚洲精品午夜无码专区| 亚洲美女在线国产| 免费h黄肉动漫在线观看| 大学生高清一级毛片免费| 曰曰鲁夜夜免费播放视频 | 中文毛片无遮挡高潮免费| 永久在线观看免费视频| 中文字幕不卡高清免费| 一级做a爰片久久毛片免费看| 国产亚洲综合一区二区三区| 亚洲国产成人综合精品| 亚洲高清视频在线| 亚洲综合色婷婷在线观看| 亚洲入口无毒网址你懂的| 亚洲fuli在线观看| 国产成人精品亚洲2020| 亚洲AV无码乱码在线观看代蜜桃| 亚洲成a人片在线网站| 亚洲妇女水蜜桃av网网站| 亚洲第一香蕉视频| 亚洲欧洲综合在线| 亚洲五月综合缴情婷婷| 中文字幕亚洲男人的天堂网络| 国产成人精品日本亚洲18图| 亚洲午夜在线播放| 亚洲av日韩av永久在线观看| 黄色网址免费在线| 国产激情久久久久影院老熟女免费| 一个人看的www视频免费在线观看|