structs2攔截器

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

          配置攔截器:Struts2中提供了大量的攔截器,多個(gè)攔截器可以組成一個(gè)攔截器棧,系統(tǒng)配置了一個(gè)默認(rèn)的攔截器棧 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>

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

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


          自定義攔截器:必須實(shí)現(xiàn) com.opensymphony.xwork2.interceptor.Interceptor 也可以繼承 AbstractInterceptor

          攔截器要保證線程安全。因?yàn)閟tructs2中攔截器會(huì)在請(qǐng)求間共享

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

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 华池县| 永和县| 浠水县| 深圳市| 进贤县| 拉萨市| 樟树市| 银川市| 库车县| 大理市| 九寨沟县| 玛沁县| 伊宁市| 会泽县| 清远市| 柘荣县| 中西区| 富民县| 莱州市| 平凉市| 越西县| 无锡市| 黄龙县| 龙泉市| 城步| 巢湖市| 南皮县| 襄城县| 锦屏县| 北安市| 渑池县| 横山县| 深州市| 富锦市| 漳平市| 武宁县| 龙泉市| 肥东县| 马鞍山市| 玉环县| 蚌埠市|