Rising Sun

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評論 :: 0 Trackbacks

          1.interceptor的配置

          方法1. 普通配置法

          <struts>
              <package name="struts2" extends="struts-default">
                  <interceptors>
                      <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
                  </interceptors>

                  <action name="register" class="edu.hust.action.RegisterAction">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                      
                      <!-- 在自定義interceptor并將其ref時, 系統會覆蓋掉默認的interceptor-stack(defaultStack), 為了保證系統默認的defaultStack不受印象, 我們需要顯式的將其引入 -->
                      <!-- 注意兩個interceptor-ref的順序, 順序不同, 執行效果也不同: 先配置的先執行/后配置的先退出(先進后出) --> 
                      <interceptor-ref name="defaultStack"></interceptor-ref> 
                      <interceptor-ref name="myInterceptor"></interceptor-ref>
                  </action>
              </package>
          </struts>
          方法2. 配置攔截器棧(即將多個interceptor串聯的一種元素)。然后在<action>中引入該攔截器棧就可以了。

          <struts>
              <package name="struts2" extends="struts-default">
                  
                  <interceptors>
                      <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
                  
                      <interceptor-stack name="myInterceptorStack">
                          <interceptor-ref name="myInterceptor"></interceptor-ref> 
                          <interceptor-ref name="defaultStack"></interceptor-ref> 
                      </interceptor-stack>
                  </interceptors>
                  
                  <action name="register" class="edu.hust.action.RegisterAction">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result> 
                       
                      <interceptor-ref name="myInterceptorStack"></interceptor-ref> 
                  </action>
              </package>
          </struts>
          方法3. 修改默認攔截器,將自定義的攔截器棧定義為struts2的默認攔截器。

          <struts>
              <package name="struts2" extends="struts-default">
                  
                  <interceptors>
                      <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
                      <interceptor-stack name="myInterceptorStack">
                          <interceptor-ref name="myInterceptor"></interceptor-ref> 
                          <interceptor-ref name="defaultStack"></interceptor-ref> 
                      </interceptor-stack>
                  </interceptors>

                  <!-- 此默認interceptor是針對所有action的 -->
                  <!-- 如果某個action中引入了interceptor, 則在這個action中此默認interceptor就會失效 -->
                  <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
                  
                  <action name="register" class="edu.hust.action.RegisterAction">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                  </action>
                  
              </package>
          </struts>
          2. Interceptor的角色對象

          (1)攔截目標對象(被代理對象),這里目標對象就是action;

          (2)攔截器(一個類,動態的將某些方法插入到目標對象的某方法的before、after);

          (3)對目標對象生成的(動態)代理對象(代理對象內部方法綜合了目標對象方法+攔截器方法)。程序最終執行的是目標對象的代理,而這個代理已經插入了interceptor。

          攔截器作用:攔截action。interceptor相當于一個入口和出口,通過interceptor進入action,執行完action的代碼再通過interceptor出去。

          針對"struts2 -- interceptor(Interceptor怎么寫)"這篇文章的MyInterceptor.class和MyInterceptor2.class。根據下面的配置文件執行程序

          <struts>
              <package name="struts2" extends="struts-default">
                  
                  <interceptors>
                      <interceptor name="myInterceptor" class="edu.hust.interceptor.MyInterceptor"></interceptor>
                      <interceptor name="myInterceptor2" class="edu.hust.interceptor.MyInterceptor2"></interceptor>
                      <interceptor-stack name="myInterceptorStack">
                          <interceptor-ref name="myInterceptor"></interceptor-ref>
                          <interceptor-ref name="myInterceptor2"></interceptor-ref>
                          <interceptor-ref name="defaultStack"></interceptor-ref>
                      </interceptor-stack>
                  </interceptors>
                  
                  <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>
                  
                  <action name="register" class="edu.hust.action.RegisterAction">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                  </action>
                  
              </package>
          </struts>

          Console會得到以下打印輸出
          intercept start
          intercept2 start
          2008-9-19 19:42:06 com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
          信息: Detected AnnotationActionValidatorManager, initializing it...
          intercept2 finish
          intercept finish

          這個結果解釋了"interceptor相當于一個入口和出口,通過interceptor進入action,執行完action的代碼再通過interceptor出去"這句話。

          3. extends MethodFilterInterceptor的攔截器如何配置哪些方法該攔截、哪些方法不該攔截(針對方法攔截的配置)

          <struts>
              <package name="struts2" extends="struts-default">
                  
                  <interceptors>
                      <interceptor name="myInterceptor3" class="edu.hust.interceptor.MyInterceptor3"></interceptor>
                  </interceptors>
                  
                  <action name="register" class="edu.hust.action.RegisterAction" method="queryAll">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                      <!-- myInterceptor3攔截器只對RegisterAction中的queryAll()方法和insert()方法進行了攔截, 其他方法未進行攔截 -->
                      <interceptor-ref name="myInterceptor3">
                          <param name="includeMethods">queryAll, execute</param>
                      </interceptor-ref>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                  </action>
                  
                  <action name="register" class="edu.hust.action.RegisterAction" method="insert">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                      <interceptor-ref name="myInterceptor3">
                          <param name="includeMethods">queryAll, insert</param>
                      </interceptor-ref>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                  </action>
                  
                  <action name="register" class="edu.hust.action.RegisterAction" method="update">
                      <result name="input">/register.jsp</result>
                      <result>/result.jsp</result>
                      <interceptor-ref name="myInterceptor3">
                          <param name="includeMethods">queryAll, insert</param>
                      </interceptor-ref>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                  </action>
                  
              </package>
          </struts>


          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/ForWayfarer/archive/2008/09/20/2955586.aspx

          posted on 2009-08-20 11:03 brock 閱讀(7522) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 南昌市| 镇巴县| 天等县| 乌海市| 岗巴县| 马鞍山市| 乌兰浩特市| 合山市| 调兵山市| 四川省| 彩票| 阿瓦提县| 兴城市| 天镇县| 新疆| 沈阳市| 安阳县| 铁岭市| 喀喇沁旗| 阿克| 宜良县| 栖霞市| 庐江县| 忻州市| 千阳县| 利津县| 武功县| 华安县| 乡城县| 营口市| 淮阳县| 和田县| 张家口市| 定西市| 额尔古纳市| 扎囊县| 南郑县| 库尔勒市| 宁蒗| 武安市| 旅游|