views:

576

answers:

5

I have an ArrayList and i am trying to display it in a table

.....

ArrayList rows = ....

.....

    <table cellspacing="1" cellpadding="4" border="3">
        <tr>
            <TH>
                Heading1
            </TH>
            <TH>
                Heading2
            </TH>
            <TH>
                Heading3
            </TH>
            <TH>
                Heading4
            </TH>
            <TH>
                Heading5
            </TH>
            <TH>
                Heading6
            </TH>
            <TH>
                Heading7
            </TH>
        </tr>

        <tr>
            <% for (int i = 0; i < rows.size(); i++) {
                for (int j = 0; j < 7; j++) {
            %>
            <td>
                <center>
                    <% out.println( ?????  ); %>
                </center>
            </td>
            <% } %>
        </tr>
        <% } %>
    </table>

but i am having trouble displaying the correct data.

+2  A: 

Well for one thing, I suspect that your outer loop should start above the <tr> tag.

Other than that, an ArrayList is a one-dimensional structure (not surprisingly, since it's a list). Trying to display this data in a table implies it's two dimensional, but without generics you've given no information as to what's contained within the list.

I'd approach this something like this:

    /* header rows */

        <% for (int i = 0; i < rows.size(); i++) { 
           Object rowObj = rows.get(i);
        %>
        <tr>

            <% for (int j = 0; j < 7; j++) {
               // This RHS made up due to not knowing the class of objects
               // in your map, use something equivalent
               Object cell = rowObj.getEntry(j); 
            %>
            <td>
                <center>
                   <%=cell.toString()%>
                </center>
            </td>
            <% } %>
        </tr>
        <% } %>
Andrzej Doyle
+1  A: 

You should not use scriptlets for this. Use JSTL/EL for this. Shortly back I've posted an example, you can find here: http://stackoverflow.com/questions/1727603/places-where-java-beans-used/1729230#1729230

BalusC
+1  A: 

It's a perfect scenario for using JSP taglibs. There is a huge list of available tablibs over at jsptags.com That way the HTML will be very readable but you'll have your dynamic table.

Kelly French
A: 

As others have pointed out you would want to use tags rather than generate a table yourself using scriptlets. The benefits are more than I care to list here. I would recommend looking at the Display tag library. It makes it trivially easy to generate a table from any Collection.

 <display:table name="rows">
    <display:column property="id" title="ID" />
    <display:column property="name" />
    <display:column property="email" />
    <display:column property="status" />
    <display:column property="description" title="Comments"/>
 </display:table>

Of course each column would refer to a property of the objects that you have in your ArrayList.

Vincent Ramdhanie
A: 

You may use core JSTL library (download it from http://jakarta.apache.org/site/downloads/downloads%5Ftaglibs.html).

Include jstl.jar and standard.jar libraries from this distribution into you class path. Then place directive <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> on top of your jsp-file. And use construction like this:

...
<table>
<c:forEach items="${rows}" var="row">
<tr>
<c:forEach items="${row}" var="column">
<td>
<c:out value="${column}"/>
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
...

Alexander Pavloshchuk