java JSP 中的 ArrayList 到表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:45:35  来源:igfitidea点击:

ArrayList to Table in JSP

javajsparraylist

提问by Dave

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

我有一个 ArrayList 并且我试图将它显示在表格中

.....

.....

ArrayList rows = ....

ArrayList 行 = ....

.....

.....

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

但我无法显示正确的数据。

回答by Andrzej Doyle

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

一方面,我怀疑您的外循环应该从<tr>标签上方开始。

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.

除此之外,ArrayList 是一个一维结构(毫不奇怪,因为它是一个列表)。尝试在表格中显示此数据意味着它是二维的,但如果没有泛型,您将无法提供有关列表中包含的内容的信息。

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

回答by BalusC

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?

您不应该为此使用 scriptlet。为此使用 JSTL/EL。不久前,我发布了一个示例,您可以在这里找到:使用 JavaBean 的地方?

回答by Kelly S. French

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

这是使用 JSP 标记库的完美场景。有超过可用tablibs在一个巨大的名单jsptags.com这样的HTML将是非常具有可读性,但你有你的动态表。

回答by Vincent Ramdhanie

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.

正如其他人指出的那样,您希望使用标签而不是使用 scriptlet 自己生成表格。好处比我想在这里列出的要多。我建议查看Display tag library。它使从任何集合生成表变得非常容易。

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

当然,每一列都将引用您在 ArrayList 中拥有的对象的一个​​属性。

回答by Alexander Pavloshchuk

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

您可以使用核心 JSTL 库(从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:

将此发行版中的 jstl.jar 和 standard.jar 库包含到您的类路径中。然后将指令<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>放在 jsp 文件的顶部。并使用这样的构造:

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