天天看點

struts2預設配置檔案 struts-default.xml 研究

今天工作不忙,研究了一下strtus2的預設配置檔案 struts-default.xml ,其中的預設的攔截器,包括其中的攔截器棧。

因為struts2的配置檔案struts.xml 中如果不配置攔截器的話,預設是basoc攔截器。如果一個action配攔截器的話,需要

需要加上另外的攔截器才能,有屬性封裝的特性,也就是能用ORGL的對象圖導航語言。

現在做的項目中用 都是這個:

   <interceptor-stack name="snzoStack">

    <interceptor-ref name="store">

     <param name="operationMode">AUTOMATIC</param>

    </interceptor-ref>

    <interceptor-ref name="paramsPrepareParamsStack" />

   </interceptor-stack>

如果用一個action配置自己定義攔截器的話,需要配置上"snzoStack"攔截器,才能用struts的自動封裝的功能。

如下是struts-default.xml的攔截器的詳細注釋:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- ....

     省略掉之前的bean的定義,直接把攔截器的配置貼出來

 ---!>

        <interceptors>

          <!-- 對于HTTP請求包含的參數設定别名。 -->

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

            <!-- 将某些JavaBean執行個體自動綁定到其他Bean對應的屬性中。有點類似Spring的自動綁定,在Spring部分會詳細說明 -->

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

            <!-- 在Web項目開發中,以前使用Struts開發時候經常碰到兩個Action互相傳遞參數或屬性的情況。該攔截器就是讓前一Action的參數可以在現有Action中使用 -->

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

            <!-- 從ActionContext中将轉化類型時候發生的錯誤添加到Action的值域錯誤中,在校驗時候經常被使用到來顯示類型轉化錯誤的資訊。 -->

            <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>

            <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />

            <!-- 自動建立一個HTTP的Session,尤其是對需要HTTP的Session的攔截器特别有用。比如下面介紹的TokenInterceptor。 -->

            <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />

            <!-- 用來對在視圖間傳遞的資料進行調試。 -->

            <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />

            <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

            <!-- 不顯式執行Action,在視圖上顯示給使用者的是一個正在等待的頁面,但是Action其實是在“背後”正在執行着。該攔截器尤其在對進度條進行開發的時候特别有用。 -->

            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

            <!-- 将異常和Action傳回的result相映射。 -->

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

            <!-- 支援檔案上傳功能的攔截器。 -->

            <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

            <!-- 支援國際化的攔截器。 -->

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

            <!-- 擁有日志功能的攔截器。 -->

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

            <!-- Action執行該攔截器時候,可以将getModel方法得到的result值放入值棧中。 -->

            <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

            <!-- 執行該攔截器時候,它可以從一個scope範圍檢索和存儲model值,通過調用setModel方法去設定model值。 -->

            <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

            <!-- 将HTTP請求中包含的參數值設定到Action中。 -->

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

            <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>

            <!-- 假如Action繼承了Preparable接口,則會調用prepare方法。 -->

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

            <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

            <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>

            <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

            <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>

            <!-- 輸出Action的執行時間。 -->

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

            <!-- 避免重複送出的校驗攔截器。 -->

            <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>

            <!-- 和token攔截器類似,但它還能存儲送出的資料到session裡。 -->

            <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>

            <!-- 運作在action-validation.xml(校驗章節将介紹)檔案中定義的校驗規則。 -->

            <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

            <!-- 在Action中調用validate校驗方法。如果Action有錯誤則傳回到input視圖。 store:執行校驗功能時候,該攔截器提供存儲和檢索Action的所有錯誤和正确資訊的功能。-->

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

            <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />

            <!-- 視圖中如果有checkbox存在的情況,該攔截器自動将unchecked的checkbox當作一個參數(通常值為“false”)記錄下來。

            這樣可以用一個隐藏的表單值來記錄所有未送出的checkbox,而且預設unchecked的checkbox值是布爾類型的,

            如果視圖中checkbox的值設定的不是布爾類型,它就會被覆寫成布爾類型的值。 -->

            <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />

            <!-- 通過參數來激活或不激活分析檢測功能,前提是Web項目是在開發模式下。(涉及到調試和性能檢驗時使用) -->

            <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />

            <!-- 進行權限配置的攔截器,如果登入使用者擁有相應權限才去執行某一特定Action。 -->

            <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />

            <interceptor name="jsonValidation" class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor" />

            <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />

            <!-- Basic stack -->

            <interceptor-stack name="basicStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo/..*,^struts/..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

            </interceptor-stack>

            <!-- Sample validation and workflow stack 校驗和工作流棧-->

            <interceptor-stack name="validationWorkflowStack">

                <interceptor-ref name="basicStack"/>

                <interceptor-ref name="validation"/>

                <interceptor-ref name="workflow"/>

            </interceptor-stack>

            <!-- Sample JSON validation stack JSON 校驗棧-->

            <interceptor-stack name="jsonValidationWorkflowStack">

                <interceptor-ref name="basicStack"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="jsonValidation"/>

                <interceptor-ref name="workflow"/>

            </interceptor-stack>

            <!-- Sample file upload stack 檔案上傳棧-->

            <interceptor-stack name="fileUploadStack">

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample model-driven stack 模型驅動棧  -->

            <interceptor-stack name="modelDrivenStack">

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample action chaining stack action 鍊棧 -->

            <interceptor-stack name="chainStack">

                <interceptor-ref name="chain"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample i18n stack i18n國際化棧-->

            <interceptor-stack name="i18nStack">

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- An example of the paramsPrepareParams trick. This stack

                 is exactly the same as the defaultStack, except that it

                 includes one extra interceptor before the prepare interceptor:

                 the params interceptor.

                 This is useful for when you wish to apply parameters directly

                 to an object that you wish to load externally (such as a DAO

                 or database or service layer), but can't load that object

                 until at least the ID parameter has been loaded. By loading

                 the parameters twice, you can retrieve the object in the

                 prepare() method, allowing the second params interceptor to

                 apply the values on the object. -->

            <interceptor-stack name="paramsPrepareParamsStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo/..*,^struts/..*</param>

                </interceptor-ref>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo/..*,^struts/..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="workflow">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

            </interceptor-stack>

            <!-- A complete stack with all the common interceptors in place.

                 Generally, this stack should be the one you use, though it

                 may do more than you need. Also, the ordering can be

                 switched around (ex: if you wish to have your servlet-related

                 objects applied before prepare() is called, you'd need to move

                 servletConfig interceptor up.

                 This stack also excludes from the normal validation and workflow

                 the method names input, back, and cancel. These typically are

                 associated with requests that should not be validated.

                 -->

            <interceptor-stack name="defaultStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="debugging"/>

                <interceptor-ref name="profiling"/>

                <interceptor-ref name="scopedModelDriven"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                  <param name="excludeParams">dojo/..*,^struts/..*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

                <interceptor-ref name="validation">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

                <interceptor-ref name="workflow">

                    <param name="excludeMethods">input,back,cancel,browse</param>

                </interceptor-ref>

            </interceptor-stack>

            <!-- The completeStack is here for backwards compatibility for

                 applications that still refer to the defaultStack by the

                 old name -->

            <interceptor-stack name="completeStack">

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

            <!-- Sample execute and wait stack.

                 Note: execAndWait should always be the *last* interceptor. -->

            <interceptor-stack name="executeAndWaitStack">

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="defaultStack"/>

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

            </interceptor-stack>

       </interceptors>

        <default-interceptor-ref name="defaultStack"/>

        <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

    </package>

</struts>

繼續閱讀