【问题标题】:Struts2 ActionContext and ValueStack?Struts2 ActionContext 和 ValueStack?
【发布时间】:2012-04-24 10:26:12
【问题描述】:

我的问题是:

  1. 在Struts2中,每个动作对象都有自己对应的ActionContext和ValueStack吗?

换句话说,对于每个新请求,都会创建一个新的操作对象。这是否意味着每次创建一个新的动作对象时,也会创建一个新的 ActionContext 和 ValueStack?

  1. 考虑这种情况:

Action1------1st req-------->view.jsp------2nd req--------->action2.

因此,当对 action1 的请求到来时,将创建一个新的 action1 对象以及相应的 ActionContext 和 ValueStack。

从 view.jsp(点击超链接)一个新的请求去 action2。

这是否意味着先前的 ActionContext 和 ValueStack(与 action1 相关)被销毁,而新的 ActionContext 和 ValueStack(与 action2 相关)被创建?

  1. 假设我在 view.jsp 中的 ActionContext(action1)中存储了一些东西,然后单击 action2 的超链接(来自 view.jsp),这些数据会与 ActionContext(action1)一起丢失吗?

【问题讨论】:

  • 也许您有一个特定的用例?如果是这样,请创建一个新问题,我们可以解释如何使用 Struts2 解决它。

标签: struts2 struts actioncontext


【解决方案1】:
  1. 是的
  2. 是的,在动作执行清理完成后。

    //SourceCode from StrutsPrepareAndExecuteFilter.
    
    //Cleans up a request of thread locals
    
    public void cleanupRequest(HttpServletRequest request) {
    
      Integer counterVal = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
      if (counterVal != null) {
          counterVal -= 1;
          request.setAttribute(CLEANUP_RECURSION_COUNTER, counterVal);
          if (counterVal > 0 ) {
              if (log.isDebugEnabled()) {
                  log.debug("skipping cleanup counter="+counterVal);
              }
              return;
          }
      }
    
      // always clean up the thread request, even if an action hasn't been executed
      ActionContext.setContext(null);
      Dispatcher.setInstance(null);
    }
    

3.是的,如果您希望该数据在下一个操作使用链中可用(不建议使用)。

【讨论】:

  • ActionContext.setContext(null);清除ThreadLocal...就像删除一个对象,但ActionContext只有一个实例。问题是没有创建新的 ActionContext,但对用户的最终影响是他们可能会这样想。
【解决方案2】:

第一季度。 ActionContext只有一个,ValueStack只有一个。

第二季度。

这是否意味着之前的ActionContext和ValueStack(相关 action1) 被销毁并且一个新的 ActionContext 和 ValueStack (对于 action2) 被创建了吗?

没有。

第三季度。我不明白这个问题。我认为缺少的是对ThreadLocal 的认识,因此尽管有一个 ActionContext,但每个线程都能够拥有自己的变量,这些变量是该线程的本地变量,因此 ValueStack 的操作范围以这种方式维护。

【讨论】:

  • 在这种情况下使用 Aware 接口是更好的选择还是?抱歉,我是这个 struts 2 的新手,没关系,如果我问的是简单的问题 :-)
  • 如果您需要某种感知接口提供的东西,我会毫不犹豫地使用它们。您的操作位于值堆栈的顶部,因此您真的不需要考虑使用它。在视图(通常是 jsp)中,您通常会想到从您的操作中提取属性,而实际上它是值堆栈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多