1

In Manning's book Struts2 in Action page 106 it is mentioned like

If you look at this in figure 5.1, you might expect that the expression would need to be something more like myAction.user.username. To the contrary, only the user.username necessary. This is because the ValueStack is a sort of virtual object that exposes the properties of its contained objects as its own.

and in the same lines it also says

If duplicate properties exist—two objects in the stack both have a name property—then the property of the highest object in the stack will be the one exposed on the virtual object represented by the ValueStack.

My doubt is if we can use user.username as described in the first paragraph, then there can be another username but of different class. Which can again be accessed with objectname.user syntax. Then why they are mentioning the problem of duplicate properties?

Roman C
  • 49,761
  • 33
  • 66
  • 176
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41

1 Answers1

0

The value stack looks like a Stack such a collection of objects that you can access at the top using push(), pop(), or peek() and it has a root, that is a compound root due XWork extension of OGNL.

The biggest addition that XWork provides on top of OGNL is the support for the ValueStack. While OGNL operates under the assumption there is only one "root", XWork's ValueStack concept requires there be many "roots".

This compound root has a list of "roots" that are traversed from top (0-index) to down the stack (size()-1) during expression evaluation until the value is found. So each root can contain the value for example user.username but the first found value will be returned. The problem is how to access those duplicated properties in other "roots". The solution is to access the root by the index. For example top object has prefixed with [0]., [1]. is an object pushed before, and so on. That's how OGNL works on the value stack. You can also see the example where I was trying access action bean properties when model driven is implemented Passing parameters to action through ModelDriven in Struts 2.

References:

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • you mean to say there can be many user instances, a case of multithreaded environment when several threads set there own user object and hence own username properties...which is more sensible.Am i going right? In that case will there be different Action contexts for different threads or will it be just one ? – jayendra bhatt Sep 12 '14 at 08:15
  • 1
    No, each thread has it's own instance of the value stack. Duplicate properties are objects references by the same name. For example if you have a `username` property in the action class and in the `User` object, then you push User object on the value stack you will have duplicate objects in the stack. If you use `username` to find the object in the value stack the highest object in the stack will be returned, i.e. `User` instance property, not the action property because User object is higher than action bean in the value stack. – Roman C Sep 12 '14 at 08:42
  • then in that case ,if there are duplicate username then either we can use user.username in the result.jsp to obtain username property of User class or we can provide [0].username...both will work fine.Am i right ? – jayendra bhatt Sep 12 '14 at 10:25
  • The name is meant by the author a key used to find a value by the value stack, and it's different in your case, however objects are also different returned by those keys unless you push the same object set to the action. – Roman C Sep 12 '14 at 11:12
  • No, you are not right, properties with different names evaluated differently. Didn't get yet? – Roman C Jan 11 '15 at 17:24
  • sorry but i didnt get that, i was saying that if there are two possible user.username values then how to retrieve them ? is it right by doing [0].user.username in that case( assuming there are two action classes with User class variable ie user) . – jayendra bhatt Jan 12 '15 at 12:04
  • 1
    you don't need to specify `[0].` prefix, but if you know the location of object in the value stack you can prepend `[n].`, where `n: 0 -> (size()-1)`. – Roman C Jan 12 '15 at 18:12