1 Reply Latest reply: Jan 23, 2012 12:24 PM by 829740 RSS

    <c:set not updating the counter\variable inside a <c:foreach

    829740
      Trying to use a counter in JSTL via <c:set.
      Out put in an xml file. It reads in a header ArrayList<String> and data ArrayList<ArrayList<String>>.
      Processes the info to produce an xml file.
      Problem is the value of counter is not updated.
      Auy ideas??? thanks

      Code
      <schoolList>
          <c:set var="head" value="${requestScope.headers}"/>
          <c:forEach var="row" items="${requestScope.data}">
              <school>
                  <c:set var="counter" value="0" scope="page"/> // set the counter at start
                  <c:forEach var="cell" items="${row}">
                      <c:out value="${counter}"/> // test = this is always 0
                      <<c:out value="${head[counter]}"/>><c:out value="${cell}"/></<c:out value="${head[counter]}"/>>
                      <c:set var="counter" value="${counter+1}" scope="page"/> // update the counter by one
                      <c:out value="${counter}"/> // test = this is always 1
                  </c:forEach>
              </school>
          </c:forEach>
      </schoolList>
      Output
      <schoolList>
         <school>
            0
            <CountyName>one</CountyName>
            1
         </school>
         <school>
            0
            <CountyName>two</CountyName>
            1
         </school>
         <school>
            0
            <CountyName>three</CountyName>
            1
         </school>
         <school>
            0
            <CountyName>four</CountyName>
            1
         </school>
      </schoolList>
      Desired Output
      <schoolList>
         <school>
            0
            <CountyName>one</CountyName>
            1
         </school>
         <school>
            1
            <Addr>two</Addr>
            2
         </school>
         <school>
            2
            <Street>three</Street>
            3
         </school>
         <school>
            3
            <Term>four</Term>
            4
         </school>
      </schoolList>