Struts2 ActionContext and ValueStack?

浪子不回头ぞ 提交于 2019-12-04 01:43:02

问题


My questions are:

  1. In Struts2, does every action object have its own corresponding ActionContext and 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. Consider this scenario:

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

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

From view.jsp (upon clicking hyperlink) a new request goes for 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. 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.Yes, If you want that data available in the next action use chain(not suggestible).




回答2:


Q1. There is one ActionContext and there is only one ValueStack.

Q2.

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

No.

Q3. I don't understand this question. What I think is missing is awareness of ThreadLocal so although there is one ActionContext each thread is able to have its own variables which are local to that thread and thus action scope for the ValueStack is maintained this way.



来源:https://stackoverflow.com/questions/10296108/struts2-actioncontext-and-valuestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!