标签:struts 栈 intercepter
本文可作为北京尚学堂struts2课程的学习笔记。既然我们研究的是拦截器那么我们先写一个action和拦截器试试。
package inter;
public class FindNameAction {
public void execute(){
System.out.println("find name!");
}
}
action没有继承任何接口,就只有一个execute。
package inter;
public interface Interceptor {
void intercept(ActionInvocation invocation);
}
package inter;
public class FirstInterceptor implements Interceptor{
@Override
public void intercept(ActionInvocation invocation) {
// TODO Auto-generated method stub
System.out.println(1);
invocation.invoke();
System.out.println(-1);
}
}
package inter;
public class SecondInterceptor implements Interceptor{
@Override
public void intercept(ActionInvocation invocation) {
// TODO Auto-generated method stub
System.out.println(2);
invocation.invoke();
System.out.println(-2);
}
}很简单吧 定义一个拦截器要实现的接口,然后有两个具体的拦截器。如果说代码里真有什么难懂的,就应该是ActionInvocaton了。别急,慢慢来。package inter;
import java.util.ArrayList;
import java.util.List;
public class ActionInvocation {
List<Interceptor> interceptors=new ArrayList<Interceptor>();
FindNameAction action=new FindNameAction();
int index=-1;
public ActionInvocation() {
// TODO Auto-generated constructor stub
interceptors.add(new FirstInterceptor());
interceptors.add(new SecondInterceptor());
}
public void invoke(){
index++;
if (index<=interceptors.size()-1)
interceptors.get(index).intercept(this);
else
action.execute();
}
}就像上面说的,ActionInvocation里面有两个变量。一个放置了所有的拦截器,一个是我们最终操作的action。package inter;
public class Test {
public static void main(String[] args) {
new ActionInvocation().invoke();
}
}下一节 我们看看源码
标签:struts 栈 intercepter
原文地址:http://blog.csdn.net/dlf123321/article/details/40078583