How does Struts2 ValueStack take care of multiple

2019-02-15 23:41发布

问题:

I understand ValueStack was introduced in Struts2 and one more change from Struts1 model is now a new ActionObject is instantiated for each request. So we can define instance variables without worrying about multi threading issues. The way interceptors and JSPs access the instance variables from the ActionObjects is through the ValueStack. But the way ValueStack is implemented (or at least used by Struts2 framework) is by ValueStack. to make the access easier so that we don't need to traverse the whole object tree. I have following questions.

  1. What if I have embedded objects (multiple hierarchy of objects) ? how does the access mechanism behave in such a case?

  2. If let us say 2 clients made requests to the same actin at the same time and the result of Action execution are different because the inputs provided by 2 clients came back with 2 different results. Let us say my Action Class has a method to get best price and based on the logic in my backend service the results come out as 10 and 12 for 2 different requests. Now ActionClass has a member variable called price in which this value will be stored and the resultant JSP showResults.jsp will access this variable (using a tag lib) to show the price. How does struts2 framework guarantee that client1 and client2 get the right response back and prices are not jumbled while the response is shown on the JSP because from what I understand the ValueStack just goes in first in first out (stack logic) fashion. So it might possibly end up returning 10 to both client requests as the same variable is stored twice (with the same name) on the value stack but with different values.

  3. When does the ValueStack destroy the object from its list?

回答1:

  1. Embedded objects or in other words nested beans are accessed directly via their accessors or via OGNL that used that accessors when evaluating OGNL expression. Struts2 places an action bean on the top of the valueStack, so the action properties are retrieved by name, but nested beans are resolved using OGNL dot notation. More about OGNL you can find in documentation.

  2. Each action instance has it's own context and valueStack, so the clients do not interfere with each other and have its own values unless the values are maintained in the application scope.

  3. The valueStack is created by the dispatcher and put to the action context. It also could be recreated by some interceptors when needed. You should not worry about its values because they are destroyed at the action end.