I am new to strut 2 though I have worked on struts 1.2.In one of the pexisting project jsp file I have following code:
<script type="text/javascript">
var relationshipData = { // line1
page : '<s:property value="displayPage" />', // line2
records : '<s:property value="customerRelations.size" />', // line3
rows : [ <s:iterator value="customerRelations" status="iterStatus"> // line4
{ id : '<s:property value="relationId" />',
cell : [ '<s:property value="relationDesc" escapeJavaScript="true" />' ] } <s:if test="!#iterStatus.last">,</s:if> //line5
</s:iterator>] // line6
};
</script>
Request is coming CustomerRelationAction.java
which has method getCustomerRelations()
and getRelationId()
.
here are the questions :-
I put breakpoint inside method
getCustomerRelations()
.i see flow is coming four time inside this method. Two times at line 3 and another two times at line 4. As per my understanding flow should come only 1 time i.e at line 3. Once it completes getCustomerRelations at line 3 , should not put its value in value stack so that it can refer to it nextime it is refered (like it is being reffered at line 14 again)?getCustomerRelations()
method returns the list ofCustomerRelationData
objects whereCustomerRelationData
class also contains thegetRelationId()
method.Now at line 5 we are refering value="relationId at line 5. On Which object(CustomerRelationAction.java or CustomerRelationData), getRelationId() method will be called? even i am not sure will the list object CustomerRelationData will be present on value stack or not?If yes at which line it will be put in value stack?Now the iterator completes at line 6.After that,now i refer the code
<s:property value="relationId" />
again, On Which object(CustomerRelationAction.java or CustomerRelationData), getRelationId() method will be called?
1) I don't know why you think calling for a property of
customerRelations
and then usingcustomerRelations
in an iterator tag would only callgetCustomerRelations()
once; you're using it twice, so at a minimum it'll get called twice.If you want to keep a reference to it, use
<s:set>
to create a new reference to the collection. I don't see a point to doing so, however, unless your getter is doing something time-consuming.I don't see the same behavior. Given the question's
<script>
snippet, it renders thusly (assuming a dummy, three-element list with sample data):And the log output, with a debug statement in the getter, is this:
I'm more likely to believe the JSP/JS/etc. at this point.
2) The iterator tag puts each object on the top of the stack, as described in the tag docs. The top of the stack is the first object that will be used to get the value of
relationId
. If it isn't found on the stack top, OGNL will traverse the value stack until either the property is found, or there's no more stack.3) See the previous answer: once you're out of the iterator, there's no longer a customer relation on the stack, and you're back to the action.