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

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

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

    ALL is Well!

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

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      30 隨筆 :: 23 文章 :: 71 評論 :: 0 Trackbacks
    本文為原創(chuàng),歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明出處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取得對應(yīng)message
                    
    // 實(shí)現(xiàn)略
                    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(
    "系統(tǒng)忙,請稍候再試。");
        }

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

    }

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

    如果非BusinessException,則統(tǒng)一顯示為 “系統(tǒ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>

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

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

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

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

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

    posted on 2010-11-16 14:53 李 明 閱讀(2808) 評論(0)  編輯  收藏 所屬分類: Struts2 、Freemarker
    主站蜘蛛池模板: 成年轻人网站色免费看| 国产羞羞的视频在线观看免费| 日韩精品无码区免费专区 | 老司机免费午夜精品视频| AV免费网址在线观看| 亚洲人成网男女大片在线播放| 最近中文字幕大全中文字幕免费| 亚洲AV成人精品网站在线播放| 国内精品久久久久影院免费| 亚洲国产另类久久久精品黑人 | 日本久久久久亚洲中字幕| 久久久久国产精品免费看| 中文字幕亚洲精品| 国产精品1024永久免费视频| 国产成人精品亚洲日本在线| 国产麻豆免费观看91| 免费高清A级毛片在线播放| 国产成人毛片亚洲精品| 暖暖在线视频免费视频| 亚洲欧洲春色校园另类小说| 在线a级毛片免费视频| 亚洲日本va午夜中文字幕久久| eeuss影院ss奇兵免费com| 日本免费v片一二三区| 又硬又粗又长又爽免费看| 亚洲国产精品成人久久| 97性无码区免费| 精品久久久久久亚洲中文字幕| 亚洲精品第一国产综合境外资源| 免费在线观影网站| 亚洲国产最大av| 亚洲国产婷婷综合在线精品| 午夜无码A级毛片免费视频| 国产亚洲玖玖玖在线观看| 久久99亚洲综合精品首页| 久久国产乱子伦精品免费看| 亚洲AV日韩综合一区尤物| 久久精品国产亚洲5555| 国产精品入口麻豆免费观看| 天堂亚洲免费视频| 亚洲另类春色校园小说|