Rising Sun

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            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時, 系統(tǒng)會覆蓋掉默認的interceptor-stack(defaultStack), 為了保證系統(tǒng)默認的defaultStack不受印象, 我們需要顯式的將其引入 -->
                      <!-- 注意兩個interceptor-ref的順序, 順序不同, 執(zhí)行效果也不同: 先配置的先執(zhí)行/后配置的先退出(先進后出) --> 
                      <interceptor-ref name="defaultStack"></interceptor-ref> 
                      <interceptor-ref name="myInterceptor"></interceptor-ref>
                  </action>
              </package>
          </struts>
          方法2. 配置攔截器棧(即將多個interceptor串聯(lián)的一種元素)。然后在<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)攔截器(一個類,動態(tài)的將某些方法插入到目標對象的某方法的before、after);

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

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

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

          <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

          這個結(jié)果解釋了"interceptor相當于一個入口和出口,通過interceptor進入action,執(zhí)行完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博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/ForWayfarer/archive/2008/09/20/2955586.aspx

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 镇坪县| 玉林市| 章丘市| 寻乌县| 锦州市| 娱乐| 汾阳市| 桑植县| 农安县| 隆昌县| 北京市| 滁州市| 开封市| 内黄县| 大竹县| 牟定县| 襄垣县| 镇江市| 郸城县| 洛宁县| 阿拉善右旗| 南乐县| 罗源县| 衡水市| 新疆| 广州市| 石狮市| 红河县| 浦江县| 宜宾市| 邢台市| 西峡县| 金平| 平和县| 灌阳县| 锦州市| 繁昌县| 安岳县| 丁青县| 博湖县| 买车|