JFinal 源码导读第三天(1) Interceptors

本文详细介绍了JFinal框架中配置全局拦截器及处理器的具体实现方式。通过实例代码展示了如何使用Interceptors类添加拦截器,以及如何通过Handlers类来配置处理器。

1.jfinalConfig.configInterceptor(interceptors);这段代码会执行我们的配置信息里面的

/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new BlogInterceptor1());
		me.add(new BlogInterceptor2());
	}
2.me.add的方法已经很简单啦,贴个代码应该一目了然
/**
 * The interceptors applied to all actions.
 */
final public class Interceptors {
	
	private final List<Interceptor> interceptorList = new ArrayList<Interceptor>();
	
	public Interceptors add(Interceptor globalInterceptor) {
		if (globalInterceptor != null)
			this.interceptorList.add(globalInterceptor);
		return this;
	}
	
	public Interceptor[] getInterceptorArray() {
		Interceptor[] result = interceptorList.toArray(new Interceptor[interceptorList.size()]);
		return result == null ? new Interceptor[0] : result;
	}
}
3.贴出BlogInterceptor1和 BlogInterceptor2
public class BlogInterceptor1 implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		System.out.println("Before invoking BlogInterceptor1" + ai.getActionKey());
		ai.invoke();
		System.out.println("After invoking BlogInterceptor1" + ai.getActionKey());
	}
}
public class BlogInterceptor2 implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		System.out.println("Before invoking BlogInterceptor2" + ai.getActionKey());
		ai.invoke();
		System.out.println("After invoking BlogInterceptor2" + ai.getActionKey());
	}
}
4.jfinalConfig.configHandler(handlers);基本上和上面初始化拦截器差不多
/**
 * 配置处理器
 */
public void configHandler(Handlers me) {
	me.add(new ContextPathHandler());
}

/**
 * Handlers.
 */
final public class Handlers {
	
	private final List<Handler> handlerList = new ArrayList<Handler>();
	
	public Handlers add(Handler handler) {
		if (handler != null)
			handlerList.add(handler);
		return this;
	}
	
	public List<Handler> getHandlerList() {
		return handlerList;
	}
}



转载于:https://my.oschina.net/skyim/blog/138235

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值