Search code examples
javajspstruts2iteratorognl

JSP iterator syntax, iterator within another


I have a syntax problem with my second iterator. I want to iterate the values of equipesParties but, I can't seem to find the right syntax.

This is the tag that causes problem: value="{equipesParties[#cpt.count()]}"

<s:iterator value="equipes" var="equipe" id="equipe" status="cpt">
    <div style="height: 30px; vertical-align: middle;">
      <input  type="checkbox" class="rEquipe" name="equipe" value="<s:property value='#equipe.id' />" id="equipe_<s:property value='#equipe.id' />" <s:if test='gallery.equipe.id == #equipe.id'>checked="checked"</s:if> />
      <label style="float: none; display: inline; padding-top: 0;" for="equipe_<s:property value='#equipe.id' />"><s:property value='#equipe.name' /></label>

    </div>

    <div id="galerie_partieBox" <s:if test='gallery == null || gallery.partie == null'>style="display: none;"</s:if> >       
        <select style="float: none; margin: 0 0 0 30px;" name="partie" id="select_partie">
         <s:if test='parties != null'>
           <s:iterator value="{equipesParties[#cpt.count()]}" var="partie">
             <option <s:if test='#partie.id == gallery.partie.id'>selected="selected"</s:if> value="<s:property value='#partie.id' />"><s:property value="#partie.gameName" /></option>
           </s:iterator>
         </s:if>
        </select>
    </div>
</s:iterator>

Solution

  • The correct syntax is equipesParties[%{#cpt.index}] , but I would step back for a second to tell you that there is no need to use an iterator to build an Select box. Just use the Select Tag provided by Struts2.

    Your (now working) code (2 s:if, 2 s:property, 1 iterator)

    <select style="float: none;margin: 0 0 0 30px;" name="partie" id="select_partie">
        <s:if test='parties != null'>
            <s:iterator value="equipesParties[%{#cpt.index}]" var="partie">
                <option <s:if test='#partie.id == gallery.partie.id'>
                            selected="selected"
                        </s:if> 
                        value="<s:property value='#partie.id' />">
                    <s:property value="#partie.gameName" />
                </option>
            </s:iterator>
        </s:if>
    </select>
    

    could be replaced by this simple tag, with an identical result

    <s:select cssStyle  = "float: none; margin: 0 0 0 30px;"
              name      = "partie"
              id        = "select_partie"
              value     = "gallery.partie.id"
              list      = "equipesParties[%{#cpt.index}]"
              listKey   = "id"
              listValue = "gameName" 
    />
    

    I omitted your <s:if test='parties != null'> because it was not valid; an Select box must have at least one option element.

    Put the <s:if> out of the <s:select/> to completely hide it, if needed.