3

我对 ActionContext 和 ActionInvocation 有点困惑。我知道 Action Context 是执行 Action 的上下文。ActionInvocation 是否位于 ActionContext 中,如 Session、Value Stack 等?其次,是否在每个用户请求上创建 ActionContext 因为它是每个线程唯一的?如果是这样,每次在每个用户请求上创建一个新的操作上下文是否成本太高?

4

2 回答 2

1

由于您知道ActionContext作为正在执行动作的上下文以及执行动作的上下文,我们还需要调用。它是 ActionContext 的一部分,您可以通过所有方式ActionInvocation从上下文中获取

public ActionInvocation getActionInvocation()

上下文使用 ThreadLocals,这意味着存储在 ActionContext 中的值对于每个线程都是唯一的。对我来说这不是任何开销,因为在其他情况下,您必须确保在多线程应用程序中访问的所有内容都是线程安全ActionContext的。将为每个请求创建一个新的。

于 2012-10-26T05:56:44.407 回答
1

Struts 2 ActionContext 和 Action Invocation 的区别

ActionContext 是在其中执行操作的对象的容器。存储在 ActionContext 中的值对于每个线程都是唯一的(即 ThreadLocal)。所以我们不需要让我们的动作线程安全。

我们可以通过调用 ActionContext 类的 getContext() 方法来获取 ActionContext 的引用。它是一种静态工厂方法。例如:ActionContext 上下文 = ActionContext.getContext();

动作调用:--

ActionInvocation 表示操作的执行状态。它包含动作和拦截器对象。

struts 框架提供了ActionInvocation 接口来处理ActionInvocation。它提供了很多方法,其中一些可以用来获取ValueStack、ActionProxy、ActionContext、Result等实例。 ActionInvocation接口的方法ActionInvocation接口常用的方法如下:

1)public ActionContext getInvocationContext() 返回与ActionInvocation 关联的ActionContext 对象。

2)public ActionProxy getProxy()返回持有这个ActionInvocation的ActionProxy实例。

3)public ValueStack getStack() 返回 ValueStack 的实例。

4)public Action getAction() 返回与此ActionInvocation 关联的Action 实例。

5)public void invoke()在处理这个ActionInvocation时调用下一个资源。

6)public Result getResult() 返回Result的实例。

于 2015-06-03T16:03:12.217 回答