ActionProxy是Action的一个代理类,也就是说Action的调用是通过ActionProxy实现的,其实就是调用了ActionProxy.execute()方法,而该方法又调用了ActionInvocation.invoke()方法。归根到底,最后调用的是DefaultActionInvocation.invokeAction()方法。
DefaultActionInvocation()->init()->createAction()。
最后通过调用ActionProxy.exute()-->ActionInvocation.invoke()-->Intercepter.intercept()-->ActionInvocation.invokeActionOnly()-->invokeAction()
这里的步骤是先由ActionProxyFactory创建ActionInvocation和ActionProxy.
-
public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) {
-
-
ActionInvocation inv = new DefaultActionInvocation(extraContext, true);
-
container.inject(inv);
-
return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext);
-
}
下面先看DefaultActionInvocation的init方法
-
public void init(ActionProxy proxy) {
-
this.proxy = proxy;
-
Map<String, Object> contextMap = createContextMap();
-
-
-
-
ActionContext actionContext = ActionContext.getContext();
-
-
if (actionContext != null) {
-
actionContext.setActionInvocation(this);
-
}
-
-
createAction(contextMap);
-
-
if (pushAction) {
-
stack.push(action);
-
contextMap.put("action", action);
-
}
-
-
invocationContext = new ActionContext(contextMap);
-
invocationContext.setName(proxy.getActionName());
-
-
-
List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
-
interceptors = interceptorList.iterator();
-
}
-
-
protected void createAction(Map<String, Object> contextMap) {
-
-
String timerKey = "actionCreate: " + proxy.getActionName();
-
try {
-
UtilTimerStack.push(timerKey);
-
-
-
-
action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);
-
} catch (InstantiationException e) {
-
throw new XWorkException("Unable to intantiate Action!", e, proxy.getConfig());
-
} catch (IllegalAccessException e) {
-
throw new XWorkException("Illegal access to constructor, is it public?", e, proxy.getConfig());
-
} catch (Exception e) {
-
...
-
} finally {
-
UtilTimerStack.pop(timerKey);
-
}
-
-
if (actionEventListener != null) {
-
action = actionEventListener.prepare(action, stack);
-
}
-
}
-
-
public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
-
Object o = null;
-
try {
-
-
o = appContext.getBean(beanName);
-
} catch (NoSuchBeanDefinitionException e) {
-
Class beanClazz = getClassInstance(beanName);
-
o = buildBean(beanClazz, extraContext);
-
}
-
if (injectInternal) {
-
injectInternalBeans(o);
-
}
-
return o;
-
}
action执行完了,还要根据ResultConfig返回到view,也就是在invoke方法中调用executeResult方法。
-
private void executeResult() throws Exception {
-
-
result = createResult();
-
-
String timerKey = "executeResult: " + getResultCode();
-
try {
-
UtilTimerStack.push(timerKey);
-
if (result != null) {
-
-
-
result.execute(this);
-
} else if (resultCode != null && !Action.NONE.equals(resultCode)) {
-
throw new ConfigurationException("No result defined for action " + getAction().getClass().getName()
-
+ " and result " + getResultCode(), proxy.getConfig());
-
} else {
-
if (LOG.isDebugEnabled()) {
-
LOG.debug("No result returned for action " + getAction().getClass().getName() + " at " + proxy.getConfig().getLocation());
-
}
-
}
-
} finally {
-
UtilTimerStack.pop(timerKey);
-
}
-
}
-
-
public Result createResult() throws Exception {
-
-
if (explicitResult != null) {
-
Result ret = explicitResult;
-
explicitResult = null;
-
-
return ret;
-
}
-
-
ActionConfig config = proxy.getConfig();
-
Map<String, ResultConfig> results = config.getResults();
-
-
ResultConfig resultConfig = null;
-
-
synchronized (config) {
-
try {
-
-
resultConfig = results.get(resultCode);
-
} catch (NullPointerException e) {
-
-
}
-
if (resultConfig == null) {
-
-
-
-
resultConfig = results.get("*");
-
}
-
}
-
-
if (resultConfig != null) {
-
try {
-
-
return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
-
} catch (Exception e) {
-
LOG.error("There was an exception while instantiating the result of type " + resultConfig.getClassName(), e);
-
throw new XWorkException(e, resultConfig);
-
}
-
} else if (resultCode != null && !Action.NONE.equals(resultCode) && unknownHandlerManager.hasUnknownHandlers()) {
-
return unknownHandlerManager.handleUnknownResult(invocationContext, proxy.getActionName(), proxy.getConfig(), resultCode);
-
}
-
return null;
-
}
-
-
public Result buildResult(ResultConfig resultConfig, Map<String, Object> extraContext) throws Exception {
-
String resultClassName = resultConfig.getClassName();
-
Result result = null;
-
-
if (resultClassName != null) {
-
-
result = (Result) buildBean(resultClassName, extraContext);
-
Map<String, String> params = resultConfig.getParams();
-
if (params != null) {
-
for (Map.Entry<String, String> paramEntry : params.entrySet()) {
-
try {
-
-
-
-
-
reflectionProvider.setProperty(paramEntry.getKey(), paramEntry.getValue(), result, extraContext, true);
-
} catch (ReflectionException ex) {
-
if (LOG.isErrorEnabled())
-
LOG.error("Unable to set parameter [#0] in result of type [#1]", ex,
-
paramEntry.getKey(), resultConfig.getClassName());
-
if (result instanceof ReflectionExceptionHandler) {
-
((ReflectionExceptionHandler) result).handle(ex);
-
}
-
}
-
}
-
}
-
}
-
-
return result;
-
}
Structs ActionProxy深度阅读
原文:http://blog.csdn.net/love_xsq/article/details/41912291