Struts2拦截器
一、什么是拦截器?
1、拦截器,在AOP(Aspect-Oriented Programming<面向切面编程>)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action 中可重用的部分的方式。
2、拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
3、如何使用struts2拦截器,或者自定义拦截器。特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了struts2自带的checkbox拦截器):
0
(必须加,否则出错)
4、也可以改为对全局Action设置自己需要的拦截器,如下:
在struts.xml里面定义全局的配置设置
0
struts-action.xml里面配置Action如下:
二、Struts2自带的配置及其拦截器配置
1、Struts2 拦截器 [Interceptor]
拦截器的工作原理如上图,每一个Action请求都包装在一系列的拦截器的内部。拦截器可以在Action
执行直线做相似的操作也可以在Action执行直后做回收操作。
每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。
2、如何自定义一个拦截器?
自定义一个拦截器需要三步:
1)自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。2)在strutx.xml中注册上一步中定义的拦截器。
3)在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截。
(1)、Interceptor接口声明了三个方法:
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception; }
A、Init方法在拦截器类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,使用这个方法可以给拦截器类做必要的初始话操作。
B、Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。
C、Intercept是拦截器的主要拦截方法,如果需要调用后续的Action或者拦截器,只需要在该方法中调用invocation.invoke()方法即可,在该方法调用的前后可以插入Action调用前后拦截器需要做的方法。如果不需要调用后续的方法,则返回一个String类型的对象即可,例如Action.SUCCESS。
另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为:public abstract class AbstractInterceptor implements Interceptor {
public void init() {
}
public void destroy() {
}
public abstract String intercept(ActionInvocation invocation) throws Exception;
}
在不需要编写init和destroy方法的时候,只需要从AbstractInterceptor继承而来,实现intercept方法即可。
例:
写一个Session过滤用的拦截器,该拦截器查看用户Session中是否存在特定的属性(LOGIN属性)
如果不存在,中止后续操作定位到LOGIN,否则执行原定操作,代码为:
public class CheckLoginInterceptor extends AbstractInterceptor {
public static final String LOGIN_KEY = "LOGIN";
public static final String LOGIN_PAGE = "global.login";
public String intercept(ActionInvocation actionInvocation)
throws Exception {
System.out.println("begin check login interceptor!");
// 对LoginAction不做该项拦截
Object action = actionInvocation.getAction();
if (action instanceof LoginAction) {
System.out.println("exit check login, because this is login action.");
return actionInvocation.invoke();
}
// 确认Session中是否存在LOGIN
Map session = actionInvocation.getInvocationContext()
.getSession();
String login = (String) session.get(LOGIN_KEY);
if (login != null && login.length() > 0) {
// 存在的情况下进行后续操作。
System.out.println("already login!");
return actionInvocation.invoke();
} else {
// 否则终止后续操作,返回LOGIN
System.out.println("no login, forward login page!");
return LOGIN_PAGE;
}
}
}
// 注册拦截器
name="login" class="com.jpleasure.teamware.util.CheckLoginInterceptor"/> 将上述拦截器设定为默认拦截器: 这样在后续同一个package内部的所有Action执行之前都会被login拦截。 三、Struts2(XWork)提供的拦截器的功能说明: 注册并引用Interceptor type="redirect-action">/secure/home 四、可以将多个拦截器合并在一起作为一个堆栈调用 当一个拦截器堆栈被附加到一个Action的时候,要想Action执行,必须执行拦截器堆栈中的每一个拦截器。 type="redirect-action">/secure/home 上述说明的拦截器在默认的Struts2应用中,根据惯例配置了若干个拦截器堆栈,详情参看struts-default.xml 其中有一个拦截器堆栈比较特殊,他会应用在默认的每一个Action上。 dojo\..* input,back,cancel,browse input,back,cancel,browse 四、每一个拦截器都可以配置参数 有两种方式配置参数,一是针对每一个拦截器定义参数,二是针对一个拦截器堆栈 统一定义所有的参数,例如: myValidationExcudeMethod myWorkflowExcludeMethod 或者 name="validation.excludeMethods">myValidationExcludeMethod name="workflow.excludeMethods">myWorkflowExcludeMethod 每一个拦截器都有两个默认的参数: excludeMethods - 过滤掉不使用拦截器的方法和 includeMethods - 使用拦截器的方法。 需要说明的几点: 1 拦截器执行的顺序按照定义的顺序执行,例如: 的执行顺序为: thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor 2 使用默认拦截器配置每个Action都需要的拦截器堆栈,例如: 可以按照如下的方式定义: 3 如何访问HttpServletRequest,HttpServletResponse或者HttpSession 有两种方法可以达到效果,使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 或者实现相应的接口: HttpSession SessionAware HttpServletRequest ServletRequestAware HttpServletResponse ServletResponseAware