JavaWeb----学习(41)----struts2----struts2拦截器

1. 拦截器(Interceptor)是 Struts 2 的核心组成部分

2.  Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载国际化数据类型转换数据校验等等。

3. Struts2 拦截器在访问某个 Action 方法之前之后实施拦截

4.  Struts2 拦截器是可插拔的, 拦截器是 AOP(面向切面编程) 的一种实现.

5.  拦截器栈(Interceptor Stack): 将拦截器按一定的顺序联结成一条链. 在访问被拦截的方法时, Struts2 拦截器链中的拦截器就

                                                    会按其之前定义的顺序被依次调用/

    拦截器工作流程:

       

JavaWeb----学习(41)----struts2----struts2拦截器JavaWeb----学习(41)----struts2----struts2拦截器

 

6. Struts2 自带的拦截器

JavaWeb----学习(41)----struts2----struts2拦截器

JavaWeb----学习(41)----struts2----struts2拦截器

7.  Interceptor 接口

      每个拦截器都是实现了 com.opensymphony.xwork2.interceptor.Interceptor 接口的 Java :

/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen ([email protected]) ***/
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import java.io.Serializable;
public abstract interface Interceptor extends Serializable {
	public abstract void destroy();
	public abstract void init();
	public abstract String intercept(ActionInvocation paramActionInvocation)throws Exception;
}

    7.1  init: 该方法将在拦截器被创建后立即被调用, 它在拦截器的生命周期内只被调用一次.

                  可以在该方法中对相关资源进行必要的初始化

   7.2 interecept: 每拦截一个请求, 该方法就会被调用一次.

   7.3  destroy: 该方法将在拦截器被销毁之前被调用, 它在拦截器的生命周期内也只被调用一次.

   7.4  Struts 会依次调用为某个 Action 而注册的每一个拦截器的 interecept 方法.

   7.5  每次调用 interecept 方法时, Struts 会传递一个 ActionInvocation 接口的实例.

   7.6   ActionInvocation: 代表一个给定 Action 执行状态, 拦截器可以从该类的对象里获得

                                       与Action 关联的 Action 对象和 Result 对象. 在完成拦截器自

                                        己的任务之后, 拦截器将调用 ActionInvocation 对象的 invoke

                                      法前进到 Action 处理流程的下一个环节.

  7.7   AbstractInterceptor 类实现了 Interceptor 接口. 并为 init, destroy 提供了一个空白的实现

                                    可以继承该类去创建一个拦截器。

 

/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen ([email protected]) ***/
package com.opensymphony.xwork2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
public abstract class AbstractInterceptor implements Interceptor {
	public AbstractInterceptor() {
	}
	public void init() {
	}
	public void destroy() {
	}
	public abstract String intercept(ActionInvocation paramActionInvocation)throws Exception;
}