天天看點

interceptor

   <interceptor name="alias" class="com.opensymphony.xwork.interceptor.AliasInterceptor"/>

   <!--不同的request的相似參數别名轉化-->

            <interceptor name="autowiring" class="com.opensymphony.xwork.spring.interceptor.ActionAutowiringInterceptor"/>

            <!--自動裝配 就是無需顯式的指定bean的合作者,由framework根據setter方法去找到相應的bean,并完成設定工作。-->

            <interceptor name="chain" class="com.opensymphony.xwork.interceptor.ChainingInterceptor"/>

            <!--将前一個執行結束的Action屬性設定到目前的Action中。它被用在ResultType為“chain”指定結果的Action中,該結果Action對象會從OgnlValueStack中獲得前一個Action對應的屬性,它實作Action鍊之間的資料傳遞;-->

            <interceptor name="component" class="com.opensymphony.xwork.interceptor.component.ComponentInterceptor"/>

            <!--激活元件功能支援,讓注冊過的元件在目前Action中可用,即為Action提供IoC(依賴倒轉控制)架構的支援;-->

            <interceptor name="conversionError" class="com.opensymphony.webwork.interceptor.WebWorkConversionErrorInterceptor"/>

            <!--用來處理架構進行類型轉化(Type Conversion)時的出錯資訊。它将存儲在ActionContext中的類型轉化(Type Conversion)錯誤資訊轉化成相應的Action字段的錯誤資訊,儲存在堆棧中。根據需要,可以将這些錯誤資訊在視圖中顯示出來。-->

            <interceptor name="createSession" class="com.opensymphony.webwork.interceptor.CreateSessionInterceptor" />

            <!--自動建立一個httpsession對象,對于某些需要有httpsession對象才能正常工作的攔截器有用-->

            <interceptor name="external-ref" class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/>

            <!--标記指定外部引用;name屬性值要與Action類中的屬性名一緻 -->

            <interceptor name="execAndWait" class="com.opensymphony.webwork.interceptor.ExecuteAndWaitInterceptor"/>

            <!--背景執行action,發送給使用者等待畫面-->

            <interceptor name="exception" class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>

            <!--提供了異常處理的主要核心功能,異常處理允許你把一個異常映射到一個結果碼-->

            <interceptor name="fileUpload" class="com.opensymphony.webwork.interceptor.FileUploadInterceptor"/>

            <!--支援檔案上傳的攔截器,參數 [file name]是被html表單中上傳得檔案的名字 maximumsize這個市允許上傳到action中的檔案最大長度(以byte為機關,注意這個參數和在webwork.properties中定義的屬性沒有關系,預設2mb)

                 allowedtype 以逗号分割的contenttype類型清單(例如text/html),這些清單示這個攔截器允許的可以上傳到action中contenttype。如果沒有指定 就是允許任何上傳類型

            <action name="doUpload" class="com.examples.UploadAction">

              <interceptor-ref name="fileUpload"/>

              <interceptor-ref name="basicstack"/>

            </action>

            -->

            <interceptor name="i18n" class="com.opensymphony.xwork.interceptor.I18nInterceptor"/>

            <!--把所選的地域放入session中

              負責把session中指定的區域設定為目前請求的區域設定,并且這個攔截器還會根據請求中的參數來改變區域設定。

              這就說你可以用來動态的改變使用者session中區域設定。這對于實作多語言支援并允許使用者在任何時候更改語言很有用。

            -->

            <interceptor name="logger" class="com.opensymphony.xwork.interceptor.LoggingInterceptor"/>

            <!--在日志資訊中輸出要執行的Action資訊-->

            <interceptor name="model-driven" class="com.opensymphony.xwork.interceptor.ModelDrivenInterceptor"/>

            <!--如果Action實作ModelDriven接口,它将getModel()取得的模型對象存入OgnlValueStack中-->

            <interceptor name="params" class="com.opensymphony.xwork.interceptor.ParametersInterceptor"/>

            <!--将Request請求的參數設定到相應Action對象的屬性中,使用者注冊例子用到過這個攔截器-->

            <interceptor name="prepare" class="com.opensymphony.xwork.interceptor.PrepareInterceptor"/>

            <!--在Action執行之前調用Action的prepare()方法,這個方法是用來準備Action執行之前要做的工作。它要求我們的Action必需實作com.opensymphony.xwork. Preparable接口

            當你需要在action中的方法執行之前執行一些邏輯。

            <!-- Calls the params interceptor twice, allowing you to pre-load data for the second time parameters are set -->

      <action name="someAction" class="com.examples.SomeAction">

       <interceptor-ref name="params"/>

       <interceptor-ref name="prepare"/>

       <interceptor-ref name="basicStack"/>

       <result name="success">good_result.ftl</result>

      </action>

            -->

            <interceptor name="static-params" class="com.opensymphony.xwork.interceptor.StaticParametersInterceptor"/>

            <!--将xwork.xml配置檔案裡定義的Action參數,設定到對應的Action中。Action參數使用<param />标簽,是<action />标簽的直接子元素。我們這裡定義的Action類必需實作com.opensymphony.xwork.config.entities. Parameterizable接口-->

            <interceptor name="scope" class="com.opensymphony.webwork.interceptor.ScopeInterceptor"/>

            <!--為了實作在webwork中實作類似向導的功能而設計的,其中的問題是,一些應用程式有些在application範圍内的公用參數,例如pagelen(每個頁面的記錄數)。有了這個攔截器就不用讓每個action都檢查這樣的參數是否存在,而是讓這個攔截器去檢查并把這些參數從session中取出

            參數:

            session 一個限制在session範圍内的action屬性清單

            application一個限制在application範圍内的屬性清單

            key 一個session/application 屬性的主鍵字首

               class 根據action的名稱空間和類來建立一個不重複的字首,這是預設值

               action根據action的名稱空間和action的名稱建立一個不重複的字首

               任意一個字元串作為主鍵字首

            type 下列支之一

               start 表示這是一個向導的開始action。所有session範圍内的屬性将被重設為預設值

               end 表示在這個action執行以後,session範圍内的屬性将會從session中清除

               any 其他值或者沒有值意思這時可一個中間action。

               <!-- ##filter#orderBy###########action#######,#############scope###. #session#############Session##########.######Application######-->

        <action name="someAction" class="com.examples.SomeAction">

        <interceptor-ref name="basicStack"/>

        <interceptor-ref name="hibernate"/>

        <interceptor-ref name="scope">

        <param name="session">filter,orderBy</param>

        <param name="autoCreateSession">true</param>

        </interceptor-ref>

            -->

            <interceptor name="servlet-config" class="com.opensymphony.webwork.interceptor.ServletConfigInterceptor"/>

            <!--

             提供Action直接對HttpServletRequest或HttpServletResponse等JavaServlet api的通路,Action要實作相應的接口,例如:ServletRequestAware或ServletResponseAware等。如果必需要提供對JavaServlet api的通路,我們建議使用ServletActionContext,在前面ActionContext章節中有介紹。

            -->

            <interceptor name="sessionAutowiring" class="com.opensymphony.webwork.spring.interceptor.SessionContextAutowiringInterceptor"/>

            <interceptor name="timer" class="com.opensymphony.xwork.interceptor.TimerInterceptor"/>

            <!--記錄Action執行的時間,并做為日志資訊輸出-->

            <interceptor name="token" class="com.opensymphony.webwork.interceptor.TokenInterceptor"/>

            <!--核對目前Action請求(request)的有效辨別,防止重複送出Action請求(request)。-->

            <interceptor name="token-session" class="com.opensymphony.webwork.interceptor.TokenSessionStoreInterceptor"/>

            <!--功能同上,但是當送出無效的Action請求辨別時,它會将請求資料儲存到session中。-->

            <interceptor name="validation" class="com.opensymphony.xwork.validator.ValidationInterceptor"/>

           <!--實作使用xml配置檔案({Action}-validation.xml)對Action屬性值進行驗證,詳細請看後面介紹的驗證架構。-->

            <interceptor name="workflow" class="com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor"/>

      <!--調用Action類的驗證功能,假設Action使用ValidationAware實作驗證(ActionSupport提供此功能),如果驗證沒有通過,workflow會将請求傳回到input視圖(Action的<result />中定義的)。-->

--------------------------------------------------------------------------------------------------------------------

1.interceptor的配置

方法1. 普通配置法

  1. <struts>
  2.     <packagename="struts2"extends="struts-default">
  3.         <interceptors>
  4.             <interceptorname="myInterceptor"class="edu.hust.interceptor.MyInterceptor"></interceptor>
  5.         </interceptors>
  6.         <actionname="register"class="edu.hust.action.RegisterAction">
  7.             <resultname="input">/register.jsp</result>
  8.             <result>/result.jsp</result>
  9.             <!-- 在自定義interceptor并将其ref時, 系統會覆寫掉預設的interceptor-stack(defaultStack), 為了保證系統預設的defaultStack不受印象, 我們需要顯式的将其引入 -->
  10.             <!-- 注意兩個interceptor-ref的順序, 順序不同, 執行效果也不同: 先配置的先執行/後配置的先退出(先進後出) -->
  11.             <interceptor-refname="defaultStack"></interceptor-ref>
  12.             <interceptor-refname="myInterceptor"></interceptor-ref>
  13.         </action>
  14.     </package>
  15. </struts>
方法2. 配置攔截器棧(即将多個interceptor串聯的一種元素)。然後在<action>中引入該攔截器棧就可以了。
  1. <struts>
  2.     <packagename="struts2"extends="struts-default">
  3.         <interceptors>
  4.             <interceptorname="myInterceptor"class="edu.hust.interceptor.MyInterceptor"></interceptor>
  5.             <interceptor-stackname="myInterceptorStack">
  6.                 <interceptor-refname="myInterceptor"></interceptor-ref>
  7.                 <interceptor-refname="defaultStack"></interceptor-ref>
  8.             </interceptor-stack>
  9.         </interceptors>
  10.         <actionname="register"class="edu.hust.action.RegisterAction">
  11.             <resultname="input">/register.jsp</result>
  12.             <result>/result.jsp</result>
  13.             <interceptor-refname="myInterceptorStack"></interceptor-ref>
  14.         </action>
  15.     </package>
  16. </struts>
方法3. 修改預設攔截器,将自定義的攔截器棧定義為struts2的預設攔截器。
  1. <struts>
  2.     <packagename="struts2"extends="struts-default">
  3.         <interceptors>
  4.             <interceptorname="myInterceptor"class="edu.hust.interceptor.MyInterceptor"></interceptor>
  5.             <interceptor-stackname="myInterceptorStack">
  6.                 <interceptor-refname="myInterceptor"></interceptor-ref>
  7.                 <interceptor-refname="defaultStack"></interceptor-ref>
  8.             </interceptor-stack>
  9.         </interceptors>
  10.         <!-- 此預設interceptor是針對所有action的 -->
  11.         <!-- 如果某個action中引入了interceptor, 則在這個action中此預設interceptor就會失效 -->
  12.         <default-interceptor-refname="myInterceptorStack"></default-interceptor-ref>
  13.         <actionname="register"class="edu.hust.action.RegisterAction">
  14.             <resultname="input">/register.jsp</result>
  15.             <result>/result.jsp</result>
  16.         </action>
  17.     </package>
  18. </struts>

2. Interceptor的角色對象

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

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

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

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

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

  1. <struts>
  2.     <packagename="struts2"extends="struts-default">
  3.         <interceptors>
  4.             <interceptorname="myInterceptor"class="edu.hust.interceptor.MyInterceptor"></interceptor>
  5.             <interceptorname="myInterceptor2"class="edu.hust.interceptor.MyInterceptor2"></interceptor>
  6.             <interceptor-stackname="myInterceptorStack">
  7.                 <interceptor-refname="myInterceptor"></interceptor-ref>
  8.                 <interceptor-refname="myInterceptor2"></interceptor-ref>
  9.                 <interceptor-refname="defaultStack"></interceptor-ref>
  10.             </interceptor-stack>
  11.         </interceptors>
  12.         <default-interceptor-refname="myInterceptorStack"></default-interceptor-ref>
  13.         <actionname="register"class="edu.hust.action.RegisterAction">
  14.             <resultname="input">/register.jsp</result>
  15.             <result>/result.jsp</result>
  16.         </action>
  17.     </package>
  18. </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的攔截器如何配置哪些方法該攔截、哪些方法不該攔截(針對方法攔截的配置)

  1. <struts>
  2.     <packagename="struts2"extends="struts-default">
  3.         <interceptors>
  4.             <interceptorname="myInterceptor3"class="edu.hust.interceptor.MyInterceptor3"></interceptor>
  5.         </interceptors>
  6.         <actionname="register"class="edu.hust.action.RegisterAction"method="queryAll">
  7.             <resultname="input">/register.jsp</result>
  8.             <result>/result.jsp</result>
  9.             <!-- myInterceptor3攔截器隻對RegisterAction中的queryAll()方法和insert()方法進行了攔截, 其他方法未進行攔截 -->
  10.             <interceptor-refname="myInterceptor3">
  11.                 <paramname="includeMethods">queryAll, execute</param>
  12.             </interceptor-ref>
  13.             <interceptor-refname="defaultStack"></interceptor-ref>
  14.         </action>
  15.         <actionname="register"class="edu.hust.action.RegisterAction"method="insert">
  16.             <resultname="input">/register.jsp</result>
  17.             <result>/result.jsp</result>
  18.             <interceptor-refname="myInterceptor3">
  19.                 <paramname="includeMethods">queryAll, insert</param>
  20.             </interceptor-ref>
  21.             <interceptor-refname="defaultStack"></interceptor-ref>
  22.         </action>
  23.         <actionname="register"class="edu.hust.action.RegisterAction"method="update">
  24.             <resultname="input">/register.jsp</result>
  25.             <result>/result.jsp</result>
  26.             <interceptor-refname="myInterceptor3">
  27.                 <paramname="includeMethods">queryAll, insert</param>
  28.             </interceptor-ref>
  29.             <interceptor-refname="defaultStack"></interceptor-ref>
  30.         </action>
  31.     </package>
  32. </struts>