struts2学习笔记(5)---自己定义拦截器

2022-12-27,,,

什么是拦截器

struts2中拦截器分为Struts2定义好的拦截器和自己定义的拦截器。

其作用是在一个Action运行之前进行拦截,在Action运行之后又增加某些操作。

实现原理

当请求一个Action时,struts2会查找配置文件,并依据这个Action的配置实例化相应的拦截器对象,然后串成一个列表(list)。最后一个一个地调用列表中的拦截器。

拦截器的运行流程

1、对Action进行预处理。(正序运行)

2、拦截器自身决定该不该运行兴许的拦截器(由invoke()方法的返回值决定)。

3、对Action进行后期处理。

(倒序运行)

源代码:自己定义拦截器

方法一:实现Interceptor接口。重写其方法

package org.Test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class TestInterceptor1 implements Interceptor { //实现Interceptor接口 public TestInterceptor1() { // 构造方法,server启动时就生成一个interceptor实例
System.out.println("TestInterceptor1 cons");
} @Override
public void destroy() { // 相似于析构方法,用于释放资源
System.out.println("TestInterceptor1 destory");
} @Override
public void init() { // server启动时被调用来初始化一些相关资源,相似于构造方法
System.out.println("TestInterceptor1 init");
} @Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor1 intercept go"); // 開始运行该拦截器
String resultName = invocation.invoke(); // 运行下一个拦截器或运行Action的execute()方法
System.out.println("TestInterceptor1 intercept back"); // 返回该拦截器 System.out.println("TestInterceptor1 resultName:" + resultName); // 打印调用的下一个拦截器或Action的返回值
return resultName;
} }

方法二:继承MethodFilterInterceptor类,重写doIntercept()方法

package org.Test.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor; public class TestInterceptor2 extends AbstractInterceptor{ //继承AbstractInterceptor,无需重写init(),destroy()方法 @Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("TestInterceptor2 intercept go");
String resultName = invocation.invoke();
System.out.println("TestInterceptor2 intercept back"); System.out.println("TestInterceptor2 resultName:" + resultName);
return resultName;
} }

源代码:配置struts.xml

<package name="default" namespace="/" extends="struts-default">

        <!-- 配置自己定义拦截器 -->
<interceptors>
<interceptor name="TestInterceptor1" class="org.Test.interceptor.TestInterceptor1"/>
<interceptor name="TestInterceptor2" class="org.Test.interceptor.TestInterceptor2"/>
</interceptors> <default-action-ref name="default" /> <action name="test" class="org.Test.action.TestAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result> <!-- 在这个Action中使用自己定义的拦截器 -->
<interceptor-ref name="TestInterceptor1"/>
<interceptor-ref name="TestInterceptor2"/>
</action> <action name="default">
<result>/index.jsp</result>
</action>
</package>

源代码:Action的定义

public class TestAction extends ActionSupport {

    @Override
public String execute() throws Exception {
System.out.println("TestAction execute");
return SUCCESS;
}
}

运行结果

1.拦截器按其在action中增加的先后顺序依次被调用。这里先调用TestInterceptor1,后调用TestInterceptor2。

2.当全部的拦截器运行完了之后才运行Action

3.然后从Action依次往上一个调用点后退。从TestAction退到TestInterceptor2,从TestInterceptor2退到TestInterceptor1。

来一张图理解

struts2学习笔记(5)---自己定义拦截器的相关教程结束。

《struts2学习笔记(5)---自己定义拦截器.doc》

下载本文的Word格式文档,以方便收藏与打印。