Struts2 ActionContext和ValueStack?

时间:2012-04-24 10:26:12

标签: struts2 struts actioncontext

我的问题是:

  1. 在Struts2中,每个操作对象都有自己的ActionContext和ValueStack吗?
  2. 换句话说,对于每个新请求,都会创建一个新的操作对象。这是否意味着每次创建新的操作对象时,都会创建一个新的ActionContext和ValueStack?

    1. 请考虑以下情况:
    2. Action1 ------ 1st req -------> view.jsp ------ 2nd req ---------> action2。

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

      从view.jsp(点击超链接时)新请求转到action2。

      这是否意味着先前的ActionContext和ValueStack(与action1相关)被破坏,并且创建了新的ActionContext和ValueStack(for action2)?

      1. 假设我在view.jsp中的ActionContext(of action1)中存储了一些内容,然后单击action2的超链接(来自view.jsp),该数据以及ActionContext(action1)是否会丢失?

2 个答案:

答案 0 :(得分:1)

Q1。有一个ActionContext,只有一个ValueStack。

Q2。

  

这是否意味着之前的ActionContext和ValueStack(与...相关)   action1)被破坏,一个新的ActionContext和ValueStack(for   action2)被创建?

没有

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

答案 1 :(得分:1)

  1. 行动执行清理后,是的。

    //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);
    }
    
  2. 3.是的,如果您希望下一个动作使用链中的数据可用(不可建议)。