Search code examples
javastruts2iteratorjstlognl

Struts2 Iterator OGNL index not working


I have this iterator.

<s:iterator value = "myQuestions"  status="key">

<s:property value="%{#key.index}" />
<h1><s:property value = "myQuestions[%{#key.index}].question"/></h1>

</s:iterator>

When the iterator iterate on this

<s:property value="%{#key.index}" />

it displays the proper index. but when I add %{#key.index} in this

 <h1><s:property value = "myQuestions[%{#key.index}].question"/></h1>

it doesn't display the element who is at that certain indice.

so let's say the current index is at 0.

when I do this <s:property value = "myQuestions[%{#key.index}].question"/> it does not display anything. but when I hard code it.

<s:property value = "myQuestions[0].question"/> it displays the proper item. what am I missing?

Solution

  • You're breaking the evaluation. It should be closer to:

    %{myQuestions[#key.index].question}
    

    It's quite redundant, though, since you're iterating, and each object in the collection will be pushed onto the stack, so all you need to refer to is question.

    You can also use the var attribute if you need to name each object.

    Consider checking out the iterator tag docs.