Discussions

Web tier: servlets, JSP, Web frameworks: <c:forEach HTML layout troubles...

  1. <c:forEach HTML layout troubles... (3 messages)

    Dear fellow Java Developers,

    I have an object in session called grid whose properties consist of several array lists. I am trying to iterate through each ArrayList so that each list has its own column side by side in one HTML table. Currently the page displays the data but does not build the table correctley. I am having a lot of trouble correctly building the HTML table. Here is my JSP code:


    <table border="1">
    <c:forEach var="cusID" items="${grid.cusID}">
    <tr>
    <td>
    <c:out value="${cusID}" />
    </td>
    </c:forEach>
    <c:forEach var="cusName" items="${grid.cusName}">
    <td>
    <c:out value="${cusName}" />
    </td>
    </tr>
    </c:forEach>
    </table>



    The CusID and CusName are printed in one long column rather then having a table with two columns side by side as desired.

    Could anyone show me a JSP code example which prints two ArrayLists in seperate columns of a HTML table?

    many thanks in advance to anyone who replies,

    James.
  2. That's really a strange requirement. Can't you have a single list with ket's say custoemr objects? So you would be able to iterate only one list and print out the attributes of the customer you like:

    <table border="1">
    <c:forEach var="customer" items="${grid.customers}">
    <tr>
    <td>
    <c:out value="${customer.id}" />
    </td>
    <td>
    <c:out value="${customer.name}" />
    </td>
    </tr>
    </c:forEach>
    </table>

    If you're not able to do it like that, try to wrap the two iterations in another table like this:

    <table border="1">
    <tr>
    <td>

    <table border="1">
    <c:forEach var="cusID" items="${grid.cusID}">
    <tr>
    <td>
    <c:out value="${cusID}" />
    </td>
    </tr>
    </c:forEach>
    </table>
    </td>

    <td>
    <table border="1">
    <c:forEach var="cusName" items="${grid.cusName}">
    <tr>
    <td>
    <c:out value="${cusName}" />
    </td>
    </tr>
    </c:forEach>
    </table>

    </td>
    </tr>
    </table>

    That would not be exactly the same layout as you wish because the rows of the two tables might not be aligned correctly.

    HTH,

    René
  3. I agree with James, I have a similar problem

    Here's some code:

    <tr>
    <c:forEach items="${sBankNumber}" var="digit">
      <td class="digitbox"><c:out value="${digit}"/></td>
    </c:forEach>
    </tr>

    If sBankNumber had "123", I would expect the output to be

    <tr>
    <td class="digitbox">1</td>
    <td class="digitbox">2</td>
    <td class="digitbox">3</td>
    </tr>

    I actually get

    <tr>
    <td class="digitbox">
    123
    </td>
    </tr>

    Are James and I missing something? This looks too much like "working as designed" - is forEach making some clever assumption about the <td> tag?

    I also tried

    <tr>
    <c:forEach items="${sBankNumber}" var="digit">
      <td class="digitbox">sss<c:out value="${digit}"/>sss</td>
    </c:forEach>
    </tr>

    This gave me

    <tr>
    <td class="digitbox">
    sss123sss
    </td>
    </tr>

    I am using Tomcat 5.1 and Java 1.4.2_04

    Any help would be appreciated

    Keith
  4. I'm afraid you misunderstood the meaning of the <c:forEach> tag. It allows you to iterate over a set of elements - e.g. a collection or an array. It definitely does not help you to split a string into its characters.

    I don't think there's an easy way to do it with tags.

    Cheers,

    René