English 中文(简体)
ArrayList to Table in JSP
原标题:

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.

问题回答

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>
        <% } %>

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?

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.

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.

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/>
...




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签