public interface Action {
public String execute() throws Exception;
}
public class TestAction implements Action {
public String execute() throws Exception {
System.out.println("action exe");
return "success";
}
}
public interface Interceptor {
public String intercept(ActionInvocation invocation) throws Exception;
}
public class Test1Intercpetor implements Interceptor{
public String intercept(ActionInvocation invocation) throws Exception{
System.out.println("hello!in test1");
String code= invocation.invoke();
System.out.println("end!in test1");
return code;
}
}
public class Test2Intercpetor implements Interceptor{
public String intercept(ActionInvocation invocation) throws Exception{
System.out.println("hello!in test2");
String code= invocation.invoke();
System.out.println("end!in test2");
return code;
}
}
public interface ActionInvocation {
public String invoke() throws Exception ;
}
public class DefaultInvocation implements ActionInvocation{
private List<Interceptor> interceptors =new ArrayList<Interceptor>();
private int index=-1;
private String result;
public String invoke() throws Exception {
if(index==interceptors.size()-1){
result=action.execute();
}else
{
index++;
result=((Interceptor)interceptors.get(index)).intercept(this);
System.out.println("????"+result);
}
return result;
}
public void addInterceptor(Interceptor interceptor){
interceptors.add(interceptor);
}
private Action action;
public Action getAction() {
return action;
}
public void setAction(Action action) {
this.action = action;
}
}
public class Test {
public static void main(String[] args){
DefaultInvocation d=new DefaultInvocation();
d.setAction(new TestAction());
d.addInterceptor(new Test1Intercpetor());
d.addInterceptor(new Test2Intercpetor());
try {
d.invoke();
} catch (Exception ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
分享到:
相关推荐
####MyBatis 插件实现分页 数据库表查询的分页 教程 本项目为简单的web。
本篇文章主要介绍了Spring MVC Interceptor 实现性能监控的功能代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
通过代理和反射实现一个Interceptor框架
利用反射和动态代理机制实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34087833
Struts2.3.6实现自定义拦截器Interceptor http://blog.csdn.net/alanchen520/article/details/34086699
6.采用二进制运算实现权限控制,利用interceptor实现粗粒度控制以及重写struts2 标签实现细粒度权限控制.采用二位运算能够使得权限运算更加快捷,而且极大 降低 session存储量,有效利用服务器资源. .系统中通过 cxf ...
struts2 Interceptor拦截器 http://whitewolf.0fees.net
Interceptor实现的原理是什么?它是怎么同Action协同工作的?为啥我在Action中定制一个变量并编写了setter/getter方法,struts2能把参数准确为我注入,咋实现的呢?为什么需要Ioc模式,它在struts2中如何体现其价值...
Interceptor实现的原理是什么?它是怎么同Action协同工作的?为啥我在Action中定制一个变量并编写了setter/getter方法,struts2能把参数准确为我注入,咋实现的呢?为什么需要Ioc模式,它在struts2中如何体现其价值...
Interceptor实现的原理是什么?它是怎么同Action协同工作的?为啥我在Action中定制一个变量并编写了setter/getter方法,struts2能把参数准确为我注入,咋实现的呢?为什么需要Ioc模式,它在struts2中如何体现其价值...
mybatis实现分页的例子,为了记录下来以便于今后自己用。
spring+springmvc+Interceptor+jwt+redis实现sso单点登录.zip
MyBatis JPA Extra对MyBatis扩展JPA功能。Jakarta JPA 3注释简化CUID操作;Interceptor实现数据库SELECT分页查询;.链式Query查询条件构造器;.提供starter,简化SpringBoot集成;数据库支持
j2ee Filter使用原理本代码里面首先是介绍了Filter实现的原理,然后模拟的了j2ee项目中Filter的使用原理,然后由此又延伸了Struts2的Interceptor实现原理,并且给予了详细的注释。
基于SpringBoot的资源请求验证(Aspectj和Interceptor两方式实现)附JWT验证token 前言 在项目中,我们需要对前端请求的资源进行验证,判断是否具有相应的权限。比如某写资源只有在登录之后才有请求权限。本章以...
建立自己的拦截器为了实现http_interceptor您需要实现InterceptorContract并创建自己的拦截器。 这个抽象类有两种方法: interceptRequest ,它在调用http请求之前触发; 和interceptResponse响应(Response
struts1 拦截器功能的实现。struts2 中的interceptor很方便。但在struts1中没有这个功能,用这个插件很容易实现。