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;

/** * 该类继承了AbstractInterceptor。而AbstractInterceptor继承的是Interceptor接口,它精简了方法.只是叫你实现一个方法就可以自己定义拦截器*/

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 {

       /*** 该自定义拦截器类针对的是对方法的action中方法的拦截

        * MethodFilterInterceptor类中:该类继承AbstractInterceptor

       * 成员变量 excludeMethods :表示不拦截那些方法  成员变量 includeMethods:   表示拦截那些方法

    * 这个对action中动态调用方法,很有效 */

       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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值