您的位置:首页 > 编程语言 > Java开发

Struts2-ActionContext

2016-03-30 15:08 645 查看
官方解释:

  The ActionContext is the context in which an {@link 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. See the {@link ThreadLocal} class for more information. The benefit of this is you don't need to worry about a user specific action context, you just get it:  

  <code>ActionContext context = ActionContext.getContext();</code>

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

  ActionContext是一个Action执行的上下文(现场).每一个Context是一个action执行所需要的数据的容器,这个容器中保存着session,parameters,locale等等.The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. 这样设计的好处就是让开发者不用担心action的使用,只需要大胆使用就可以了,无需来保证它的线程安全.

   原来我们所取得的ctx来自于 ThreadLocal 啊!熟悉ThreadLocal 的朋友都知道它是与当前线程绑定的 

Struts2自定义的一个数据的容器.ActionContext是个map集合,它持有了web标准的4个域(pagecontext域,request域,session域,application域(servlet开发 context)),通过它可以直接获取这四个标准容器。

不仅如此,它还引用了其它struts 2自己定义的对象:



ActionContext的几个特点:

1,ActionContext是strtuts2新设计出来的存储数据的容器,该容器是个map集合

它里面持有到达各个容器(其它map)的引用,如可以获取标准的4个域容器,还有struts 2自己定义的东西,如valuestack值栈

2,我们可以通过 ActionContext方便的获取到其它容器,如同一个快捷方式,struts 2的数据中心

3,ActionContext是threadloacl线程绑定的, 当struts 2接受到请求后,就会马上创建一个ActionContext,然后各个位置都可以获取到ActionContext

4,ActionContext意思就是action运行的上下文,负责存储action运行需要/产生的数据

5,ActionContext它里面有个map集合用来存储数据,但是ActionContext本身作为一个对象,它也有很多其它开发方法

ActionContext.getcontext() :获取ActionContext里的大map集合;ActionContext.getcontext().getvaluestack() //获取值栈

[参考]

  http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E7%89%9B/32881.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: