设置编码的具体实现。嗯,代码写的蛮好…

/**
* Creates the action context and initializes the thread local
*/
public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
// 计数器,作用:记录这个Action被访问的次数,与后续释放ActionContext的内存空间有关,初始化为1
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
// 如果request域里面已经有一个计数器,说明这个Action已经被实例化调用过了,那么就将这个计数器的值加1
if (oldCounter != null) {
counter = oldCounter + 1;
}
// 和上面的计数器类似,尝试从request获取这个Action的上下文对象,如果存在就直接使用这个ActionContext
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 {
// 不存在就创建一个值栈(存储了与这个Action相关的一些属性)和一个ActionContext
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
ctx = new ActionContext(stack.getContext());
}
// 把这个计数器放到request域里面
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
// 跟进这个方法后有这么个注释:Sets the action context for the current thread(把这个Action上下文对象放入当前线程)
ActionContext.setContext(ctx);
return ctx;
}
这个计数器可是有点用处:它用来记录一个Action被调用的次数。那么为什么要记录它被调用的次数呢?这里先提前看一下doFilter方法的最后一步:prepare.cleanupRequest(request);这一步是用来清理掉该次请求所占用的内存,跟进源码:
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-78438-4.html
0现在是9
不听话放出来就得下课