4 Replies Latest reply on May 12, 2010 5:26 AM by igorg

    Nested <c:forEach> causes errors

    igorg

      I try to create sample component representing weekly calendar with CDK version 3.3.3.Final. Most of the things waorks fine, but there are some problems in generated code.

       

      First case:

       

      {code:xml}

      <c:set var="hourIndex" value="1"/>

      <c:forEach var="trName" items="#{this:getHoursList( component )}">

           <c:set var="hourIndex" value="${hourIndex+1}"/>  

           <tr>

           <td class="hour" scope="row">#{trName}</td>

           <c:forEach var="dayIndex" items="#{this:getDayIndexes( component )}">

       

       

       

       

           <td>

       

       

       

       

                 <jsp:scriptlet><![CDATA[

                     variables.setVariable(

                          "slotVal",

                          component.getDataModel().getItemFromSlot(

                                     (Integer)variables.getVariable( "dayIndex" ),

                                    (Integer)variables.getVariable( "hourIndex" )

                               ).getValue() );

                     ]]></jsp:scriptlet>

                 ${slotVal}

           </td>

           </c:forEach>

           </tr>

      ....

       

       

       

       

      {code}

       

       

       

       

       

      The code above is from jspx template and it failes with 'iter is already defined in doEncodeBegin(....' error message. I checked generated renderer class and saw that two nested 'for( Iterator iter....' lines are generated by CDK plugin. Is it the bug or I've done something wrong?

       

      Second case:

      I tried to change code to escape two iterators declarations and changed:

      {code:xml}

      <c:object var="daysCount" type="java.lang.Integer"
           value="#{component.dataModel.endDay-component.dataModel.startDay+1}"/>

      <c:forEach var="dayIndex" begin="0" end="#{daysCount}" step="1">

      ....

      </c:forEach>

      {code}

       

       

       

       

       

      causes 'Cannot find symbol. symbol  : method getInteger(java.lang.Integer) location: class java.lang.Integer' error

      inspecting of generated renderer shows :

       

      {code}

      for (

      variables.setVariable("dayIndex", "0");

      Integer.getInteger(variables.getVariable("dayIndex").toString()).intValue() < Integer.getInteger(daysCount).toString().intValue();

      variables.addValueToVariable("dayIndex", new Integer(1) )

      )

      ...

      {code}

       

      It is clear that the error occured beacuse daysCount is Integer, but changing its type to String causes error in c:object.

       

       

      Now I've replaced forEach tags with java scriplets and it works, but the idea of template is to use tags rather then java scriplets.

      Can anybody show me right usage of nested forEach tags with CDK, please?

        • 1. Re: Nested <c:forEach> causes errors
          nbelaevski

          Igor,

           

          I haven't checked this yet, but looks like nested c:forEach are not supported well. You can use scriptlets anyway...

          • 2. Re: Nested <c:forEach> causes errors
            igorg

            Yes Nick. Y use scriplet now. I've asked this question beacause I don't like to mix tags with java code, but if I need it, I do. However, nested loops is common thing, may be develpoers of CDK will add it new versions. I tried some other tags to escape this problems. ui:repeat seems to work well, but it isn't in list of supported template tags at CDK Developer Guide and I don't know if it'll work well in all cases. I've mentioned in my original question that I made another try using forEach with start/end/step arguments in one of the loops. Such approach allows to escape duplicate variables problem, but it seems that forEach with start/end/step has serious limitations. See second part of my original question, if it is intresting for you.

             

            Igor.

            • 3. Re: Nested <c:forEach> causes errors
              nbelaevski

              Igor,

               

              We are fully redesigning CDK as part of our 4.x work, so it should be fixed there. Does workaround with scriptlets suit you well?

              1 of 1 people found this helpful
              • 4. Re: Nested <c:forEach> causes errors
                igorg

                Yes Nick, thanks. It looks ugly, but works fine.