Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access ValueStack objects within Struts iterator?

I have 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 which contain details of a review, such as rating and name of user.

My problem is that I'm not able to access any of the objects present on the ValueStack within the loop.

Outside the loop <s:property value="#request.restaurant.portalImage.url" /> works correctly. But within the loop it prints null.

AFAIK an iterator pushes it's collection on the ValueStack so that all OGNL expressions resolve against it. But I've used # which means I'm explicitly specifying the root object for resolution.

Why is it still not working?

like image 511
Monika Michael Avatar asked Apr 12 '12 11:04

Monika Michael


1 Answers

I just spent my whole afternoon fighting against a similar issue. In my case, the issue was that my iterator variable (In your case reviews) had a field with the same name as the outer variable.

No matter how hard I tried to break out of the local stack of the iterator, it would not access the outer variable.

If your reviews iterator has a field called restaurant its going to override the outer restaurant variable, no matter how hard you try :(

The solution I found was to simply rename the outer variable. (eg: theRestaurant)

Once I renamed it, it was no longer clashing with the inner variable, and I was able to access it from within the iterator.

like image 59
Lenny Markus Avatar answered Sep 24 '22 10:09

Lenny Markus