0

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.

Dave
  • 11
  • 1
  • 1
  • 1
  • How is your arraylist structured ? – Soufiane Hassou Nov 13 '09 at 14:01
  • 1
    Are you asking what should go in the ????? part, or are you asking why it isn't displaying correctly? If the latter, I noticed that your opening TR tag needs to go within the first loop. – Jeff Nov 13 '09 at 14:03

5 Answers5

3

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
  • 102,507
  • 33
  • 189
  • 228
1

You should not use scriptlets for this. Use JSTL/EL for this. Shortly back I've posted an example, you can find here: Places where JavaBeans are used?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

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 S. French
  • 12,198
  • 10
  • 63
  • 93
0

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
  • 102,349
  • 23
  • 137
  • 192
0

You may use core JSTL library (download it from http://jakarta.apache.org/site/downloads/downloads_taglibs.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:

...  
<br/>
<table><br/>
<c:forEach items="${rows}" var="row"><br/>
<tr><br/>
<c:forEach items="${row}" var="column"><br/>
<td><br/>
<c:out value="${column}"/><br/>
</td><br/>
</c:forEach><br/>
</tr><br/>
</c:forEach><br/>
</table><br/>
...
Hari
  • 1,509
  • 15
  • 34