Interceptors是jfinal aop的實(shí)現(xiàn)方式,通過(guò)實(shí)現(xiàn)Interceptor接口以及使用@Before可以
精確進(jìn)行配置,
Interceptor接口僅僅定了一個(gè)方法void intercept(ActionInvocation ai);
我們可以讓一個(gè)類(lèi)實(shí)現(xiàn)這個(gè)接口,重寫(xiě)方法,如:
public class DemoInterceptor implements Interceptor {
public void intercept(ActionInvocation ai) {
System.out.println("Before action invoking");
ai.invoke();
System.out.println("After action invoking");
}
}
就寫(xiě)好了一個(gè)攔截器。
攔截器配置有三個(gè)級(jí)別,global級(jí),controller級(jí),action級(jí)。global級(jí)的攔截器將對(duì)所有的
action進(jìn)行攔截,controller級(jí)攔截器將對(duì)該controller中的所以action攔截,action級(jí)攔截器
只對(duì)該action進(jìn)行攔截。
global級(jí)攔截器在
public void configInterceptor(Interceptors me) {
me.add(new DemoInterceptor());
}
中配置,controller級(jí)攔截器使用@Before放在controller類(lèi)定以前進(jìn)行配置,action級(jí)攔截器
使用@Before放在action定義前進(jìn)行配置。具體配置如下:
@Before(DemoInterceptor.class) // 配置一個(gè)Controller級(jí)別的攔截器
public class HelloController extends Controller {
@Before(AaaInterceptor.class)
public void index() {
renderText("配置一個(gè)action級(jí)別的攔截器");
}
@Before({AaaInterceptor.class, BbbInterceptor.class})
public void test() {
renderText("配置多個(gè)action級(jí)別的攔截器");
}
@ClearInterceptor
public void login() {
renderText("清除上一級(jí)別(Controller級(jí))的攔截器");
}
@ClearInterceptor(ClearLayer.ALL)
public void clearAllLayers() {
renderText("清除所有級(jí)別(Global級(jí)與Controller級(jí))的攔截器");
}
}