至於Action是的創建則是由ActionProxy來完成的,來看一段簡要的程序調用
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack()); //調用ActionInvocation
proxy.execute();
其實 ActionProxy是一個接口,而ActionProxyFactory則是一個抽象類,他們都是通過一個DefaultActionProxy和DefaultActionProxyFactory來完成操作的,且ActionProxy將調用ActionInvocation接口,由DefaultActionInvocation初始化的時候讀取配置,然後由Invoke()方法來完成Action的調用及一些在Action被調用之前的Interceptor的操作.下面是關於DefaultActionInvocation的初始化和調用代碼.
public class DefaultActionInvocation implements ActionInvocation {
private void init() throws Exception {
Map contextMap = createContextMap();
createAction(); //加載Action
if (pushAction) {
stack.push(action);
}
invocationContext = new ActionContext(contextMap);
invocationContext.setName(proxy.getActionName());
// get a new List so we don't get problems with the iterator if someone changes the list
List interceptorList = new ArrayList(proxy.getConfig().getInterceptors()); //獲取配置
interceptors = interceptorList.iterator();
}
public String invoke() throws Exception {
if (executed) {
throw new IllegalStateException("Action has already executed");
}
//這裡是執行攔截器的操作, 注: 攔截器本身就是AOP的一個特殊實現,Servlet2.3 中Filter就是一個特例啊
if (interceptors.hasNext()) {
Interceptor interceptor = (Interceptor) interceptors.next();
resultCode = interceptor.intercept(this);
} else {
resultCode = invokeAction(getAction(), proxy.getConfig());
}
// this is needed because the result will be executed, then control will return to the Interceptor, which will
// return above and flow through again
if (!executed) {
if (preResultListeners != null) {
for (Iterator iterator = preResultListeners.iterator();
iterator.hasNext();) {
PreResultListener listener = (PreResultListener) iterator.next();
listener.beforeResult(this, resultCode);
}
}
// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
executeResult();
}
executed = true;
}
…
}
下面再來說說Interceptor 的實現結構,剛開始我以為XWork1.x中Interceptor 應該是從Filter中繼承下來的,後來看了源碼,原來我的想法不對,想想也的確是不需要,也不應該從Filter下繼承,因為Filter就是Servlet2.3的一個API,而XWork1.x設計目的就是要脫離Servlet API,且Interceptor的實現並非是少了Filter就不行,只是我們有了Filter將會來的更加方便!
對於WebWork2.x中的所有的攔截器,他們都有一個公共的接口Interceptor,在它當中定義了攔截器的一些基本操作方法,然後有一個AroundInterceptor抽象類,實現了該接口, AroundInterceptor的作用是組合攔截器的調用順序,代碼如下:
public String intercept(ActionInvocation invocation) throws Exception {
String result = null;
before(invocation); //這裡是用於組合調用順序
result = invocation.invoke();
after(invocation, result);
return result;
}
至於將Map 中的數據轉換到我們的VO中,是通過ParametersInterceptor攔截器來完成操作的,這個攔截器是一個真正的實現類,他從AroundInterceptor抽象類下面繼承
public class ParametersInterceptor extends AroundInterceptor {
//~ Methods ////////////////////////////////////////////////////////////////
protected void after(ActionInvocation dispatcher, String result) throws Exception {
}
protected void before(ActionInvocation invocation) throws Exception {
if (!(invocation.getAction() instanceof NoParameters)) {
final Map parameters = ActionContext.getContext().getParameters();
//用於獲取Map 結構中的Parameters
if (log.isDebugEnabled()) {
log.debug("Setting params " + parameters);
}
ActionContext invocationContext = invocation.getInvocationContext();
try {
invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);
invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE);
invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
if (parameters != null) {
final OgnlValueStack stack = ActionContext.getContext().getValueStack();
//用於獲取OgnlValueStack操作,這個package沒看過,具體聽夏昕說是一套可讀寫對象屬性的的類庫,功能有些類似與Jakarta Commons BeanUtils ,及Spring Bean Wrapper
for (Iterator iterator = parameters.entrySet().iterator();
//遍歷Parameters中的信息
iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String name = entry.getKey().toString();
//填充VO信息
if (acceptableName(name)) {
Object value = entry.getValue();
stack.setValue(name, value);
}
}
}
} finally {
invocationContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE);
invocationContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.FALSE);
invocationContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);
}
}
}
protected boolean acceptableName(String name) {
if (name.indexOf('=') != -1 || name.indexOf(',') != -1 || name.indexOf('#') != -1) {
return false;
} else {
return true;
}
}
}