How to access ValueStack objects in a Struts iterator?
I have the following code:
<s:iterator value="reviews"> <img src="<s:property value="#request.restaurant.portalImage.url" />" /> <s:property value="user.firstName" /> <s:property value="user.lastName" /> <s:property value="rating" /> <s:property value="review" /> </s:iterator>
reviews is a list of review objects that contain viewing information, such as rating and username.
My problem is that I cannot access any of the objects present in the ValueStack in the loop.
Outside of the loop, <s:property value="#request.restaurant.portalImage.url" />
works correctly. But inside the loop, it prints zero.
The AFAIK iterator pushes it on a ValueStack so that all OGNL expressions resolve it. But I used #, which means that I explicitly specify the root object for permission.
Why is it still not working?
I just spent the whole day struggling with a similar problem. In my case, the problem was that my iterator variable (in your case reviews
) had a field with the same name as the external variable.
No matter how hard I tried to break out of the local iterator stack, it will not access an external variable.
If your iterator review has a field called a restaurant, it will redefine the external variable of the restaurant , no matter how you try: (
I found a solution to just rename an external variable. ( e.g .: theRestaurant )
As soon as I renamed it, it no longer ran into an internal variable, and I was able to access it from inside the iterator.
You should change it as below:
<s:iterator value="reviews" var="varrequest"> <img src="<s:property value="#varrequest.restaurant.portalImage.url" />" /> <s:property value="#varrequest.user.firstName" /> <s:property value="#varrequest.user.lastName" /> <s:property value="#varrequest.rating" /> <s:property value="#varrequest.review" /> </s:iterator>
Using an iterator reference variable to retrieve a unique index object.
I'm not sure which object is contained in the collection that you use in Iterator. As an iterator, when you iterate a collection using the S2 iterator tag, it will push the object at the top of the value stack, which means, say, you have a collection of a custom object on which you iterate, for example
<s:iterator value="userCollection"> </s:iterator>
therefore, when it will be an iteration, S2 iterator will push the user
object on top of the value stack, and if we want to refer to the name
property of the user
object, all we need is to refer to the name property
<s:property name="userName"/>
since OGNL will try to resolve userName
in the value stack, and we already have the user object as a top-level object in the value stack. I suggest going through the documentation of the Iterator tag and how it clicks values
I think it solves your problem
s:iterator value="reviews" status="position"> <img src="<s:property value="#request.restaurant.portalImage.url"/>"/> <s:property value="#position.user.firstName"/> <s:property value="#position.user.lastName"/> <s:property value="#position.rating"/> <s:property value="#position.review"/> </s:iterator>
Your requirement is a little confusing