Struts2系列教程之拦自定义拦截器实例源码介绍



Struts2系列教程之拦自定义拦截器实例源码介绍,实现拦截器,主要有以下三种方法:

l         实现Interceptor接口

Interceptor接口有三个方法:

public interface Interceptor extends Serializable {

void destroy();

void init();

String intercept(ActionInvocation invocation) throws Exception;


}

l         继承AbstractInterceptor抽象类

AbstractInterceptor实现了Interceptor接口,但这个抽象类对“destroy”和“init”方法进行了重写,但在方法中没有执行任何操作。主要用于不需要执行其他初始化或清理动作。

l         继承MethodFilterInteceptor抽象类

MethodFilterInteceptor继承AbstractInterceptor类,主要用与对Action方法过滤的拦截器。有集合两个属性:includeMethods和excludeMethods,includeMethods表示:包含Action方法,作用是拦截器只对包含方法进行拦截,其他不进行拦截,可以配置多个,以逗号隔开。excludeMethods表示:不包含Action方法,作用是拦截器对不包含方法不进行拦截,其他进行拦截,可以配置多个,以逗号隔开。

注意:如果一个方法即在includeMethods又在excludeMethods属性中,includeMethods优先于excludeMethods。