以下代码模拟Struts2的interceptor实现
附件是struts2源码分析
1. 定义接口
public interface Interceptor
{
String intercept(ActionInvocation invocation) throws Exception;
}
2.接口实现
public class InterceptorA implements Interceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
System.out.println("begin A");
invocation.invoke();
System.out.println("before interceptor's resultCode : " + invocation.getResultCode());
System.out.println("end A");
return "A";
}
}
public class InterceptorB implements Interceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
System.out.println("begin B");
invocation.invoke();
System.out.println("before interceptor's resultCode : " + invocation.getResultCode());
System.out.println("end B");
return "B";
}
}
public class InterceptorC implements Interceptor
{
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
System.out.println("begin C");
invocation.invoke();
System.out.println("before interceptor's resultCode : " + invocation.getResultCode());
System.out.println("end C");
return "C";
}
}
3.Invoke 调用
public class ActionInvocation
{
Iterator<Interceptor> InterceptorIterator = null;
private String resultCode = null;
protected boolean executed = false;
public String invoke()
{
if (executed)
{
throw new IllegalStateException("Action has already executed");
}
if (InterceptorIterator.hasNext())
{
Interceptor interceptor = InterceptorIterator.next();
try
{
resultCode = interceptor.intercept(ActionInvocation.this);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return "fuck";
}
public Iterator<Interceptor> getIt()
{
return InterceptorIterator;
}
public void setIt(Iterator<Interceptor> it)
{
this.InterceptorIterator = it;
}
public Iterator<Interceptor> getInterceptorIterator()
{
return InterceptorIterator;
}
public void setInterceptorIterator(Iterator<Interceptor> interceptorIterator)
{
InterceptorIterator = interceptorIterator;
}
public String getResultCode()
{
return resultCode;
}
public void setResultCode(String resultCode)
{
this.resultCode = resultCode;
}
}
4 调用
public static void main(String[] args)
{
ActionInvocation invocation = new ActionInvocation();
List<Interceptor> list = new ArrayList<Interceptor>();
Interceptor a = new InterceptorA();
Interceptor b = new InterceptorB();
Interceptor c = new InterceptorC();
list.add(a);
list.add(b);
list.add(c);
Iterator<Interceptor> it = list.iterator();
invocation.setIt(it);
invocation.invoke();
System.out.println("end");
}
运行结果:
begin A
begin B
begin C
before interceptor's resultCode : null
end C
before interceptor's resultCode : C
end B
before interceptor's resultCode : B
end A
end
分享到:
相关推荐
struts2源码详细解析51CTO下载-struts2源代码分析(个人觉得非常经典)
struts2源码分析总结 是我在网上找到的关于源码讲解比较深刻的资料总结。对于研究struts2源码很有价值。
Struts2源码分析 请求首先通过Filter chain,Filter 主要包括ActionContextCleanUp,它主要清理当前线 程的ActionContext 和Dispatcher;FilterDispatcher 主要通过AcionMapper 来决定需要调用哪 个Action。
Struts2源码分析--请求处理.pdf
Struts2源码分析--请求处理[汇编].pdf
Struts2源码阅读
有关struts2运行原理的源码分析,附加相关的类图
读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验,那么千万不要想当然地...虽然Struts2的开发小组极力保留Struts1.x的习惯,但因为Struts2的核心设计完全改变,从思想到设计到工作流程,都有了很大的不同。
struts 1 源码分析 struts1.2
主要介绍了Struts2源码分析之ParametersInterceptor拦截器,ParametersInterceptor拦截器其主要功能是把ActionContext中的请求参数设置到ValueStack中,,需要的朋友可以参考下
struts详细的源码分析,运行过程,想深入学习struts的小伙伴可以参看
唉,咋下载东西都要积分呢,没办法了,只能逼着我要一分了,自己写的笔记,研究了源码,不过不太深入,大致思路还是对的
本文主要对Struts的源代码进行分析,因为Struts2与WebWork的关系如此密不可分,因此,读者需要下载xwork的源代码,访问http://www.opensymphony.com/xwork/download.action即可自行下载。 下载的Struts2源代码文件...
Struts2框架的简单回顾和源码分析 Strtus2框架,是基于MVC开发模式的表现层框架 作用可以说和servlet相同,作为页面和java代码之间数据交互的桥梁
struts2的源码分析,主要是执行了哪些方法,以文字的方式进行了描述。
Struts+Spring源码分析(喜欢分析源码的有福了),Struts+Spring 源码分析文档.