天天看点

struts2-拦截器

strusts2的拦截器底层采用的是动态代理机制实现的。而我们只是需要在struts.xml配置文件中配置拦截器就可以实现了。自定义拦截器会和struts.xml文件一起随着服务器的启动而加载。

对于strust2的拦截器它是针对的是表单提交数据的一种过滤机制,一个自己定义的过滤器可以针对一个action,也可以针对多个action。

1.       自定义拦截器的定义的第一种写法:自定义拦截器是要继承Interceptor这个struts给的拦截器接口。我们只需要实现的它的里面的三个方法。

比如说:import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

//这个定义的成员变量,可以从strust.xml文件中传递值过来

private String message;

public MyInterceptor() {}

public void destroy() {  

      System.out.println("destory");

System.out.println(this.message);}

public void init()

{     System.out.println("init");}

public String intercept(ActionInvocation invocation) throws Exception {

              String result = invocation.invoke();

    return result;}

    public String getMessage() {

       return message;  }

    public void setMessage(String message) {

       this.message = message;  }}

在struts.xml文件中:

    <package name="login"  extends="struts-default">

      <!-- 自己定义的拦截器 -->

      <interceptors>

        <interceptor name="myInterceptor" class="org.interceptor.MyInterceptor">

         <param name="message">  hello  </param> </interceptor>

        <!—自定义拦截器栈-à

        <interceptor-stack name="myinterceptor">

        <interceptor-ref name="myInterceptor"></interceptor-ref>

        </interceptor-stack>  </interceptors>

      <!-- 在action中,加了自定义的拦截器,还要加默认的"defaultStack"拦截器栈,因为你引入其他的拦截器的时候,就不默认引入"defaultStack" -->  

       <action name="login" class="org.www.RegistertAction">

            <interceptor-ref name="myInterceptor">

             <param name="message">china</param>

            </interceptor-ref>

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

           <result name="input">/login.jsp</result>

           <result>/success.jsp</result>

       </action></package> 

注意的事项:  1、在声明的时候加入了给该自定义的拦截器类的成员变量赋值,在action中执行的时候,也赋值了。最后得到的是值action中执行的时候赋值的那个值。

2、每个包体下默认的action都有一个拦截器是

  <default-interceptor-ref name="defaultStack"/>,而当如果你在action中引入了自定义的拦截器,那么在这个action中还要手动配置” defaultStack”。

3、拦截器栈里可以套多个拦截器。也可以套多个拦截器栈。

2、自定义拦截器的第二种写法:

package org.interceptor;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor1 extends AbstractInterceptor {

    public MyInterceptor1() {   }

    public String intercept(ActionInvocation invocation) throws Exception {

       String result = invocation.invoke();

       return result;  }}

在struts.xml文件中的配置和第一种的是一致的

3、对action中方法的拦截  import com.opensymphony.xwork2.ActionInvocation;

                                               import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

                                               public class MyInterceptor2 extends MethodFilterInterceptor {

       public MyInterceptor2() {          }

       protected String doIntercept(ActionInvocation invocation) throws Exception {

              String result = invocation.invoke();    return result;   }}

在struts.xml文件的配置<package name="login"  extends="struts-default">

       <interceptors>

        <interceptor name="myInterceptor2" class="org.interceptor.MyInterceptor2">

        </interceptor>

       <action name="login!*" class="org.www.RegistertAction" method="{1}">

 <interceptor-ref name="myInterceptor2">

<!-- 不拦截的方法 --> <param name