jayendra bhatt jayendra bhatt - 1 month ago 9
Java Question

Duplicate properties in the Struts ValueStack

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 ?

Answer

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:

Comments