自定义拦截器

来源:互联网 发布:js调用另一个js的函数 编辑:程序博客网 时间:2024/06/03 13:56

自定义拦截器,class类要继承AbstractInterceptor,Struts2中的拦截器都是继承它,实现intercept方法:

package com.interceptor;
 import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor{     @Override    public String intercept(ActionInvocation invocation) throws Exception {        System.out.println("before");        String result = invocation.invoke();        System.out.println("after");        return result;    }  }

Struts.xml中的配置

    <interceptors>        <interceptor name="MyInterceptor" class="com.interceptor.MyInterceptor"></interceptor>     </interceptors>


在action中调用时应先用自定义的,再用默认的
    <interceptor-ref name="MyInterceptor"></interceptor-ref>    <interceptor-ref name="defaultStack"></interceptor-ref>



0 0
原创粉丝点击