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

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

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

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0

     


    什么是攔截器

         攔截器,在AOP(Aspect-Oriented Programming)中用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實現策略。

         在Webwork的中文文檔的解釋為——攔截器是動態攔截Action調用的對象。它提供了一種機制可以使開發者可以定義在一個action執行的前后執行的代碼,也可以在一個action執行前阻止其執行。同時也是提供了一種可以提取action中可重用的部分的方式。

         談到攔截器,還有一個詞大家應該知道——攔截器鏈(Interceptor Chain,在Struts 2中稱為攔截器棧Interceptor Stack)。攔截器鏈就是將攔截器按一定的順序聯結成一條鏈。在訪問被攔截的方法或字段時,攔截器鏈中的攔截器就會按其之前定義的順序被調用。

    實現原理

         Struts 2的攔截器實現相對簡單。當請求到達Struts 2的ServletDispatcher時,Struts 2會查找配置文件,并根據其配置實例化相對的攔截器對象,然后串成一個列表(list),最后一個一個地調用列表中的攔截器,如圖1所示。

    圖1 攔截器調用序列圖
    圖1 攔截器調用序列圖

    已有的攔截器

        Struts 2已經為您提供豐富多樣的,功能齊全的攔截器實現。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看關于默認的攔截器與攔截器鏈的配置。

    數據加載中……

       首先,要跟大家道個歉,前一陣子為給客戶個一個DEMO,忙得不可開交,所以很久沒有更新Blog。提到這個DEMO我想順便跟大家分享一下心得——如果大家希望快速開發,一個類似Struts 2這樣的簡單方便的WEB框架必不可少。我們在開發DEMO使用的還是Struts 1.2.8,而且沒有不使用任何EL(表達式語言),導致頁面出現無數類似

    “<%= ((Integer) request.getAttribute("xx")).intValue()%6 %>”

    的代碼。Struts 1.x的Form Bean的麻煩使得有部分同事直接使用request.getParameter(String arg),繼而引入另一種麻煩。諸如此類的問題,在DEMO這樣時間緊迫的項目凸顯了Struts 1.x對快速開發的無能為力。不過沒辦法,由于我們項目中的幾個資深員工除了Struts 1.x外,對其它的WEB框架似乎不大感興趣。

        言歸正傳,Interceptor(以下譯為攔截器)是Struts 2的一個強有力的工具,有許多功能(feature)都是構建于它之上,如國際化轉換器校驗等。

    在本文使用是Struts 2的最新發布版本2.0.1。需要下載的朋友請點擊以下鏈接:
    http://apache.justdn.org/struts/binaries/struts-2.0.1-all.zip

    以下部分就是從struts-default.xml文件摘取的內容:

    < interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />
    < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />
    < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />
    < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />
    < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />
    < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
    < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />
    < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />
    < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />
    < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />
    < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />
    < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />
    < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />
    < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />
    < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />
    < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />
    < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />
    < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />
    < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />
    < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />
    < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />
    < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />
    < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
    < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />
    < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
    < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />
    < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />
    < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

    配置和使用攔截器

         在struts-default.xml中已經配置了以上的攔截器。如果您想要使用上述攔截器,只需要在應用程序struts.xml文件中通過“<include file="struts-default.xml" />”將struts-default.xml文件包含進來,并繼承其中的struts-default包(package),最后在定義Action時,使用“<interceptor-ref name="xx" />”引用攔截器或攔截器棧(interceptor stack)。一旦您繼承了struts-default包(package),所有Action都會調用攔截器棧 ——defaultStack。當然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆蓋defaultStack。

    下面是關于攔截器timer使用的例子。首先,新建Action類tuotrial/TimerInterceptorAction.java,內容如下:

    package tutorial;

    import com.opensymphony.xwork2.ActionSupport;

    public class TimerInterceptorAction extends ActionSupport {
       @Override
       
    public String execute() {
           
    try {
               
    // 模擬耗時的操作
               Thread.sleep( 500 );
           }
    catch (Exception e) {
               e.printStackTrace();
           }

           
    return SUCCESS;
       }

    }

    配置Action,名為Timer,配置文件如下:

    <! DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
            "http://struts.apache.org/dtds/struts-2.0.dtd"
    >
    < struts >
       
    < include file ="struts-default.xml" />    
       
    < package name ="InterceptorDemo" extends ="struts-default" >
           
    < action name ="Timer" class ="tutorial.TimerInterceptorAction" >
               
    < interceptor-ref name ="timer" />
               
    < result > /Timer.jsp </ result >
           
    </ action >
       
    </ package >
    </ struts >

    至于Timer.jsp可以隨意寫些什么到里面。發布運行應用程序,在瀏覽器的地址欄鍵入http://localhost:8080/Struts2_Interceptor/Timer.action,在出現Timer.jsp頁面后,查看服務器的后臺輸出。

    2006 - 12 - 6 14 : 27 : 32 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
    信息: Executed action
    [ //Timer!execute ] took 2859 ms.

        在您的環境中執行Timer!execute的耗時,可能上述的時間有些不同,這取決于您PC的性能。但是無論如何,2859 ms與500 ms還是相差太遠了。這是什么原因呢?其實原因是第一次加載Timer時,需要進行一定的初始工作。當你重新請求Timer.action時,以上輸出會變為:

    2006 - 12 - 6 14 : 29 : 18 com.opensymphony.xwork2.interceptor.TimerInterceptor doLog
    信息: Executed action
    [ //Timer!execute ] took 500 ms.

           OK,這正是我們期待的結果。上述例子演示了攔截器timer的用途——用于顯示執行某個action方法的耗時,在我們做一個粗略的性能調試時,這相當有用。



    posted on 2007-11-24 07:47 々上善若水々 閱讀(2324) 評論(1)  編輯  收藏 所屬分類: Struts2

    評論

    # re: 使用Struts2自帶的攔截器  回復  更多評論   

    參考部分:http://50vip.com/blog.php?i=189
    2013-04-16 14:56 | 紅色石頭
    主站蜘蛛池模板: 日韩电影免费在线观看网站| 久久香蕉国产线看观看亚洲片| 人人玩人人添人人澡免费| 中文字幕亚洲情99在线| 久久精品国产亚洲香蕉| 亚洲国产日韩在线观频| 午夜一级毛片免费视频| 四虎免费影院ww4164h| 国产麻豆成人传媒免费观看| 免费人妻精品一区二区三区| 亚洲日韩乱码中文字幕| 亚洲国产美女精品久久| 老司机午夜在线视频免费观| 亚洲伊人久久精品| 日韩亚洲AV无码一区二区不卡| 国产自偷亚洲精品页65页| 国产精品免费视频一区| 久久WWW色情成人免费观看| 91嫩草免费国产永久入口| 国产成人精品无码免费看| 两性色午夜免费视频| 人妻免费久久久久久久了| 亚洲成在人天堂在线| 久久久久亚洲av成人无码电影| 四虎永久免费地址在线观看| 免费无码看av的网站| 四虎影院免费视频| 成年女人免费视频播放77777| 99精品国产免费久久久久久下载| 中文字幕免费在线| 1000部无遮挡拍拍拍免费视频观看 | 亚洲VA成无码人在线观看天堂| 国产精品亚洲αv天堂无码| 亚洲熟妇少妇任你躁在线观看无码| 国产免费av片在线播放| 又粗又硬又黄又爽的免费视频 | 亚洲精品一区二区三区四区乱码| 亚洲高清在线视频| 亚洲黄网在线观看| 亚洲五月综合网色九月色| 亚洲中文字幕乱码熟女在线|