Two dimensional array + c:foreach

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lisagh81
    New Member
    • Aug 2007
    • 2

    Two dimensional array + c:foreach

    Hello

    I have a servlet that creates results in a two dimentional array and then uses request.setAttr ibute to store it.

    for example :
    String[][] fullname = {{"name1", "surname1"},{"n ame2", "surname2"},{"n ame3", "surname3"} };

    request.setAttr ibute("fullname ",fullname) ;

    Then JSP page will use JSTL c:foreach to display it:

    <c:forEach var = "itemarr2" items = "${fullname }">
    <tr>

    <td>${itemarr 2[0]}</td>
    <td>${itemarr 2[1]}</td>
    </tr>
    </c:forEach>

    The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

    Or may be another tag or method to solve this problem ?

    Thank you
    Lisa
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by lisagh81
    Hello

    I have a servlet that creates results in a two dimentional array and then uses request.setAttr ibute to store it.

    for example :
    String[][] fullname = {{"name1", "surname1"},{"n ame2", "surname2"},{"n ame3", "surname3"} };

    request.setAttr ibute("fullname ",fullname) ;

    Then JSP page will use JSTL c:foreach to display it:

    <c:forEach var = "itemarr2" items = "${fullname }">
    <tr>

    <td>${itemarr 2[0]}</td>
    <td>${itemarr 2[1]}</td>
    </tr>
    </c:forEach>

    The above is working fine, except that in this case I know the array has two columns, but in many cases I don't know how many columns I have. Is there a way to make column numbers dynamic ?

    Or may be another tag or method to solve this problem ?

    Thank you
    Lisa
    Well, you do know that itemarr2 is an array so you can use plain old Java to get
    the number of elements in that array: itemarr2.length . You can use plain old
    loops if you want or the JSTL loop.

    kind regards,

    Jos

    Comment

    • lisagh81
      New Member
      • Aug 2007
      • 2

      #3
      Originally posted by JosAH
      Well, you do know that itemarr2 is an array so you can use plain old Java to get
      the number of elements in that array: itemarr2.length . You can use plain old
      loops if you want or the JSTL loop.

      kind regards,

      Jos

      Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

      Any examples on how to implement this is extremely appreciated.

      Lisa

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by lisagh81
        Thank you for your answer ..... Could you kindly give a hint on how to use itemarr2.length inside the c:foreach to accomplish this ?

        Any examples on how to implement this is extremely appreciated.

        Lisa
        I don't speak JSP JSTL tags, but I guess it can be something like this:

        Code:
        <c:forEach var = "name" items = "${itemarr2}">
        // do something with name here
        kind regards,

        Jos

        Comment

        Working...