本文介绍了Struts2 ActionContext和ValueStack?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:

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

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

In other words, for every new request a new action object is created. Does this mean every time a new action object is created, a new ActionContext and ValueStack also get created?

  1. 考虑这种情况:

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

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

因此,当请求action1时,将创建action1的新对象以及相应的ActionContext和ValueStack.

So when a request comes for action1 a new object of action1 and corresponding ActionContext and ValueStack will get created.

在view.jsp中(单击超链接时),有一个新的请求将请求action2.

From view.jsp (upon clicking hyperlink) a new request goes for action2.

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

Does this mean that previous ActionContext and ValueStack (related to action1) gets destroyed and a new ActionContext and ValueStack (for action2) gets created?

  1. 假设我将某些内容存储在view.jsp中的(动作1的)ActionContext中,然后单击动作2的超链接(来自view.jsp),那么该数据和(动作1的)ActionContext是否会丢失?
  1. Suppose I store something in ActionContext (of action1) in view.jsp and then click on hyperlink for action2 (from view.jsp), will that data along with the ActionContext (of action1) get lost?

推荐答案

  1. 是,在执行动作后将进行清理.

  1. Yes
  2. Yes after action execution clean up will be done.

//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.是的,如果您希望在下一个操作中使用该数据,请使用链(不建议使用).

3.Yes, If you want that data available in the next action use chain(not suggestible).

这篇关于Struts2 ActionContext和ValueStack?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 00:31