3

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?
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Siva
  • 3,297
  • 7
  • 29
  • 35
  • Perhaps you have a specific use case in mind? If so, create a new question and we can explain how to go about it with Struts2. – Quaternion Apr 24 '12 at 18:34

2 Answers2

1

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.

Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • Using Awareinterfaces are the better choice in this case or ? sorry am new to this struts 2, never mind if am asking simple question :-) – Siva Apr 27 '12 at 09:33
  • If you need something one of the aware interfaces provides I wouldn't hesitate to use them. Your action is placed at the top of the value stack, so you really don't need to think about using it. In the view (most often jsp) you generally think of extracting properties from your action when I reality it is the value stack. – Quaternion Apr 27 '12 at 20:14
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).

orique
  • 1,295
  • 1
  • 27
  • 36
MohanaRao SV
  • 1,117
  • 1
  • 8
  • 22
  • ActionContext.setContext(null); Clears the ThreadLocal... it is like deleteing an object, but there is only one instance of ActionContext. The issue is that a new ActionContext is not created, but the net effect for a user is that they might as well think of it that way. – Quaternion Apr 24 '12 at 18:17