struts ActionContext ThreadLocal

public class ActionContext implements Serializable

   The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action

needs for execution like the session, parameters, locale, etc.

   The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. The benefit

of this is you don't need to worry about a user specific action context, you just get it:

                                  ActionContext context = ActionContext.getContext();

Finally, because of the thread local usage you don't need to worry about making your actions thread safe.

1.源码分析

 1 public class ActionContext implements Serializable {
 2     static ThreadLocal actionContext = new ThreadLocal();
 3     
 4     Map<String, Object> context;
 5 
 6     
 7     public ActionContext(Map<String, Object> context) {
 8         this.context = context;
 9     }
10 
11     public static ActionContext getContext() {
12         return (ActionContext) actionContext.get();
13 
14         // Don't do lazy context creation, as it requires container; the creation of which may 
15         // precede the context creation
16         //if (context == null) {
17         //    ValueStack vs = ValueStackFactory.getFactory().createValueStack();
18         //    context = new ActionContext(vs.getContext());
19         //    setContext(context);
20         //}
21 
22     }
23 
24 
25     public void setContextMap(Map<String, Object> contextMap) {
26         getContext().context = contextMap;
27     }
28 
29     //.....
30 
31 }

 

posted @ 2013-08-04 16:18  等风来。。  Views(339)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------