struct2的拦截器


struts2事先在struts-default.xml中定义了许多拦截器和适合不同用途的拦截器栈

拦截器 名字 说明 
                      
下面是struts2提供的一些自带拦截器栈
     
<interceptors> 
           <!--省略了拦截器定义--> 
            <!-- 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"/> 
        <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 -->
    <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 -->
    <interceptor-stack name="chainStack">
        <interceptor-ref name="chain"/>
        <interceptor-ref name="basicStack"/>
    </interceptor-stack>

            <!-- Sample i18n stack -->
    <interceptor-stack name="i18nStack">
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="basicStack"/>
    </interceptor-stack>

           
    <interceptor-stack name="paramsPrepareParamsStack">
        <interceptor-ref name="exception"/>
        <interceptor-ref name="alias"/>
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="params"/>
        <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"/>
        <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 
                 servlet-config 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="prepare"/> 
        <interceptor-ref name="i18n"/> 
        <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\..*</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>
struts-default包默认的拦截器栈为defaultStack,因为其他自定义的包全继承自该包,所以我们自己定义的package的默认拦截器栈都为defaultStack。可以在package中添加一下语句指定默认的拦截器栈
<default-interceptor-ref name="myStack"/>
注意:如果我们要使用自己定义的栈,struts2会只使用我们定义的拦截器而不会执行框架默认的拦截器。所以自定义拦截器栈时一定要记得将系统的默认栈加进去,如下所示
<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>success.jsp</result> 
    </action> 
</package> 
注意:定义拦截器栈时要注意拦截器的顺序,因为某些拦截器会中断stack/chain/flow

你可能感兴趣的:(struct)