日期:2014-05-19 浏览次数:20667 次
prepare.createActionContext(request, response);这个里面有创建 ActionContext 你好好看下这个方法里面的代码:
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
ActionContext.setContext(ctx); return ctx;
ActionContext.setContext(ctx);这个很关键。跟进去会发现 actionContext.set(context);。actionContext对象其实是ThreadLocal对象,然后你会发现你后来取得ActionContenxt的时候都是通过
public static ActionContext getContext() {呵呵大致懂了吧 把生成的ActionContext对象先放在ThreadLocal对象中等到要用的时候再取出来用。。。。其实不是取出来的原来的而是原来的一个副本具体可以看下ThreadLocal这个类的用法。。。。
return (ActionContext) actionContext.get();