详细代码:http://download.csdn.net/detail/tanxiang21/5402599
模拟Struts2的工作责任链过程,接口调用invoke方法执行
public interface ActionInvocation{
Object getAction();
boolean isExecuted();
Result getResult() throws Exception;
String getResultCode();
void setResultCode(String resultCode);
String invoke() throws Exception;
String invokeActionOnly() throws Exception;
}
接口实现类,这里具体的流程执行内容,由外部传入,不同于Struts2才有容器注入方式
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
public class DefaultActionInvocation implements ActionInvocation {
protected Iterator<InterceptorMapping> interceptors;
protected Action action = new SelfAction();
@SuppressWarnings("rawtypes")
private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
protected Result result;
protected String resultCode;
protected boolean executed = false;
//这里我们外部传入内容,Struts采用inject注入以及调用init方法初始化内容
public DefaultActionInvocation(List<InterceptorMapping> interceptorsTree,
Action action) {
this.interceptors = interceptorsTree.iterator();
this.action = action;
}
public String invoke() throws Exception {
if (executed) {
throw new IllegalStateException("Action has already executed");
}
//遍历拦截器
if (interceptors.hasNext()) {
final InterceptorMapping interceptor = interceptors.next();
resultCode = interceptor.getInterceptor().intercept(
DefaultActionInvocation.this);
}
//执行Action
else{
resultCode = invokeActionOnl