Struts2中拦截器的配置

        在struts.xml文件中定义拦截器只需要给拦截器类指定一个拦截器名,这就完成了定义。拦截器使用<interceptor>标记来定义,格式如下

<interceptor name="拦截器名" class="拦截器类"></interceptor>

大部分情况下,如果有一个拦截器这样配置就够了。如果有多个拦截器,则需要写多个<interceptor>,而<interceptor>是写在<interceptors>标记内部,而<interceptors>又是写在struts.xml中的<package>标记中的
<package ...>
  <interceptors>
     <interceptor name="拦截器名1" class="拦截器类1"></interceptor>
     <interceptor name="拦截器名2" class="拦截器类2"></interceptor>
     <interceptor name="拦截器名3" class="拦截器类3"></interceptor>
     ...
  </interceptors>
</package>
如果需要还可以再拦截器<interceptor》标记中间加上参数标记,用来给拦截器定义参数,参数标记是<param>.格式如下
<interceptor name="拦截器名" class="拦截器类">
   <param name="参数名">参数值</param>
</interceptor>
其中的参数可以有0个,也可以有多个。这里定义的多个拦截器参数可以再拦截器类中直接获取。获取的方法是在拦截器类中定义名称和参数相同的字段,还要定义字段的seter方法。例如在struts.xml的配置文件中定义如下:
<interceptors>
 <interceptor name="helloInterceptor" class="com.inter.HelloInterceptor">
   <param name="name">李四</param>
 </interceptor>
</interceptors>


拦截器类HelloInterceptor.java如下:
package com.inter;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class HelloInterceptor extends AbstractInterceptor {

   private static final long serialVersionUID = 1L;
    //上面配置拦截器时的参数name
    private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("the interceptor is running...");
String result = arg0.invoke();
System.out.println("获取拦截器中的参数信息是"+name);
System.out.println("the interceptor is ending...");
return result;
}
}

输出的信息如下:
the interceptor is running...
正在执行action...
获取拦截器中的参数信息是李四
the interceptor is ending...


可以将多个定义好的拦截器组织成一个拦截器栈,拦截器栈使用<interceptor-stack>标记定义。配置拦截器栈的格式如下:
<interceptor-stack name="拦截器栈名">
    <interceptor-ref name="拦截器1"/>
    <interceptor-ref name="拦截器2"/>
    <interceptor-ref name="拦截器3"/>
    ...
</interceptor-stack>
下面是Struts2中核心配置文件struts-default.xml中的部分代码,从中我们可以看出拦截器栈的配置方式

        <interceptors>
            <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
            <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
            <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
            <interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
            <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"/>
            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
            <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"/>


            <!--省略部分代码-->


   <!-- 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="multiselect"/>
                <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>
            
   <!--省略部分代码-->
            
            <!-- 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="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <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="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>


            <!-- 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="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="multiselect"/>
                <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>

            <!--省略部分代码-->

       </interceptors>

其中,多个已经定义的拦截器栈构成拦截器栈。拦截器栈不仅可以由基本拦截器构成,还可以由其他的拦截器栈构成,例如:
<interceptor-stack name="拦截器栈名1">
    <interceptor-ref name="拦截器1"/>
    <interceptor-ref name="拦截器2"/>
    <interceptor-ref name="拦截器3"/>
    ...
</interceptor-stack>
<interceptor-stack name="拦截器栈名2">
    <interceptor-ref name="拦截器栈1"/>
    <interceptor-ref name="拦截器栈2"/>
    <interceptor-ref name="拦截器栈3"/>
    ...
</interceptor-stack>
上面两个拦截器栈,其中第一个拦截器栈是由三个拦截器构成,而第二个拦截器栈是由三个拦截器栈构成

使用拦截器
拦截器的使用相对比较简单,只需要在struts.xml文件的action中添加<interceptor-ref>标记,用于指定具体使用哪
一个定义好的拦截器即可。
<action name="helloaction" class="com.action.HelloAction">
<result name="success">/success.jsp</result>
<result name="input">/reg.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="helloInterceptor"></interceptor-ref>
</action>

其中<interceptor-ref name="defaultStack"></interceptor-ref>是系统的默认拦截器,当用户自定义了拦截器后,系统默认的拦截器会自动失效。为了能够使用系统默认的拦截器,应该把它加上。


你可能感兴趣的:(Interceptor配置,Struts2中拦截器配置)