模仿Struts2的Interceptor拦截器实现
模仿Struts2的Interceptor拦截器实现
public interface Invocation {
public Object invoke();
}
public interface Interceptor {
public Object intercept(Invocation invocation);
}
public class Target {
public Object execute() {
System.out.println("Targer.execute()");
return "Targer-execute";
}
}
public class InvocationChain implements Invocation {
private Iterator<Interceptor> iterator = null;
private Target target = new Target();
public void init(List<Interceptor> interceptors) {
List<Interceptor> interceptorsList = new ArrayList<Interceptor>(interceptors);
this.iterator = interceptorsList.iterator();
}
public Object invoke() {
if(iterator.hasNext()) {
return iterator.next().intercept(this);
} else {
return target.execute();
}
}
}
public class IntOne implements Interceptor {
public Object intercept(Invocation invocation) {
Object result = null;
System.out.println("IntOne.intercept()-begin");
result = invocation.invoke();
System.out.println("IntOne.intercept()-end");
return result;
}
}
public class IntTwo implements Interceptor {
public Object intercept(Invocation invocation) {
Object result = null;
System.out.println("IntTwo.intercept()-begin");
result = invocation.invoke();
System.out.println("IntTwo.intercept()-end");
return result;
}
}
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
List<Interceptor> interceptors = new ArrayList<Interceptor>();
interceptors.add(new IntOne());
interceptors.add(new IntTwo());
InvocationChain chain = new InvocationChain();
chain.init(interceptors);
System.out.println(chain.invoke());
}
}
模仿Struts2的Interceptor拦截器实现的更多相关文章
- struts2自定义Interceptor拦截器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
理解 Interceptor拦截器类似于我们学过的过滤器,是可以在action执行前后执行的代码.是我们做web开发是经常使用的技术,比如权限控制,日志.我们也可以把多个interceptor连在一起 ...
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- 5.Struts2中的拦截器
拦截器是Struts2中的核心,其自带很多很多的拦截器,这里主要介绍一下自定义拦截器,恩多一半情况下呢?我们不需要使用到自定义的拦截器,Struts2本身已经提 供了很多的拦截器供我们使用,对于自定义 ...
- struts2基础——自定义拦截器
一.自定义拦截器 默认的拦截器能实现的功能是有限的,Struts2 支持自定义拦截器. 二.拦截器类 1.实现 Interceptor 接口 2.继承 AbstractInterceptor 抽象类, ...
- Struts2(十四)拦截器实现权限管理
一.认识拦截器 拦截器也是一个类 拦截器可以在Action被调用之前和之后执行代码 框架很多核心功能是拦截器实现的 拦截器的特点: 拦截器自由组合,增强了灵活性.扩展性.有利于系统解耦 拦截器可以拦截 ...
- Spring MVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- 十五、struts2中的拦截器(框架功能核心)
十五.struts2中的拦截器(框架功能核心) 1.过滤器VS拦截器 功能是一回事. 过滤器是Servlet规范中的技术,可以对请求和响应进行过滤. 拦截器是Struts2框架中的技术,实现AOP(面 ...
- SpringMVC 中的Interceptor 拦截器
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
随机推荐
- HDUOJ---(4708)Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- &&和;和||符号的意思
http://www.cnblogs.com/xuxm2007/archive/2011/01/16/1936836.html在命令行可以一次执行多个命令,有以下几种: 1.每个命令之间用;隔开 ...
- Windows下面安装和配置Solr 4.9(三)支持中文分词器
首先将下载解压后的solr-4.9.0的目录里面F:\tools\开发工具\Lucene\solr-4.9.0\contrib\analysis-extras\lucene-libs找到lucene- ...
- 配置Kafka集群和zookeeper集群
原文链接请参见:http://www.cnblogs.com/5iTech/articles/6043224.html
- OpenCV245之SURF源代码分析
一.fastHessianDetector函数分析 (1)參数 const Mat& sum 积分图片 const Mat& mask_sum vecto ...
- WinForm开发,窗体显示和窗体传值相关知识总结
主窗体中代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- Vue 最传统的新增行,删除行,提交的数据整合
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- js 倒计时 (时分秒版本)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- find命令之exec和xargs
exec: find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是c ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...