structs2攔截器

          深入struct2攔截器  這篇文章很好,細致講解了structs2和攔截器的原理。
          http://zhanghong.iteye.com/blog/452465
          轉載在每次對你的 Action的 execute()方法請求時,系統會生成一個 ActionInvocation對象,這個對象保存了 action和你所配置的所有的攔截器以及一些狀態信息。比如你的應用使用的是 defaultStack,系統將會以攔截器棧配置的順序將每個攔截器包裝成一個個 InterceptorMapping(包含攔截器名字和對應的攔截器對象 )組成一個 Iterator保存在 ActionInvocation中。在執行 ActionInvocation的 invoke()方法時會對這個 Iterator進行迭代,每次取出一個 InterceptorMapping,然后執行對應 Interceptor的 intercept(ActionInVocation inv)方法,而 intercept(ActionInInvocation inv)方法又包含當前的 ActionInInvcation對象作為參數,而在每個攔截器中又會調用 inv的 invoke()方法,這樣就會進入下一個攔截器執行了,這樣直到最后一個攔截器執行完,然后執行 Action的 execute()方法 (假設你沒有配置訪問方法,默認執行 Action的 execute()方法 )。在執行完 execute()方法取得了 result后又以相反的順序走出攔截器棧,這時可以做些清理工作。最后系統得到了一個 result,然后根據 result的類型做進一步操作。

          配置攔截器:Struts2中提供了大量的攔截器,多個攔截器可以組成一個攔截器棧,系統配置了一個默認的攔截器棧 defaultStack,具體包括那些攔截器以及順序可以在struts-default.xml中找到。
          1)
          <package name="default" extends="struts-default">
             <interceptors>
                 <interceptor name="timer" class=".."/>
                 <interceptor name="logger" class=".."/>
             </interceptors>

             <action name="login"
                class="tutorial.Login">
                  <interceptor-ref name="timer"/>
                  <interceptor-ref name="logger"/>
                   <result name="input">login.jsp</result>
                   <result name="success"
                      type="redirectAction">/secure/home</result>
             </action>
          </package>

          2)
          <package name="default" extends="struts-default">
             <interceptors>
                  <interceptor name="timer" class=".."/>
                  <interceptor name="logger" class=".."/>
                  <interceptor-stack name="myStack">
                     <interceptor-ref name="timer"/>
                     <interceptor-ref name="logger"/>
                 <interceptor-ref name="defaultStack"/>    
                  </interceptor-stack>
              </interceptors>

          <action name="login"
               class="tutuorial.Login">
                   <interceptor-ref name="myStack"/>
                   <result name="input">login.jsp</result>
                   <result name="success"
                       type="redirectAction">/secure/home</result>
          </action>
          </package>

          攔截器執行順序:
          <interceptor-stack name="xaStack">
            <interceptor-ref name="thisWillRunFirstInterceptor"/>
            <interceptor-ref name="thisWillRunNextInterceptor"/>
            <interceptor-ref name="followedByThisInterceptor"/>
            <interceptor-ref name="thisWillRunLastInterceptor"/>
          </interceptor-stack>

          執行順序:
          thisWillRunFirstInterceptor
            thisWillRunNextInterceptor
              followedByThisInterceptor
                thisWillRunLastInterceptor
                  MyAction1
                  MyAction2 (chain)
                  MyPreResultListener
                  MyResult (result)
                thisWillRunLastInterceptor
              followedByThisInterceptor
            thisWillRunNextInterceptor
          thisWillRunFirstInterceptor


          自定義攔截器:必須實現 com.opensymphony.xwork2.interceptor.Interceptor 也可以繼承 AbstractInterceptor

          攔截器要保證線程安全。因為structs2中攔截器會在請求間共享

          posted on 2011-11-08 18:35 AK47 閱讀(1447) 評論(0)  編輯  收藏 所屬分類: Structs

          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 五华县| 黑山县| 贡嘎县| 噶尔县| 安图县| 广宗县| 阳山县| 抚松县| 那坡县| 都江堰市| 南阳市| 上犹县| 龙川县| 咸阳市| 措勤县| 久治县| 高平市| 竹山县| 莱州市| 太湖县| 敖汉旗| 蓬溪县| 东阿县| 沛县| 昌宁县| 巴南区| 肇源县| 都匀市| 岱山县| 青铜峡市| 石门县| 江西省| 盐津县| 连山| 长沙县| 定结县| 藁城市| 阿拉善左旗| 革吉县| 星座| 津南区|