如何以表格格式显示ArrayList?

发布于 2025-01-02 09:05:46 字数 262 浏览 0 评论 0原文

我的 Servlet 代码中有一个 ArrayList。现在,我想以表格格式显示 ArrayList。我怎样才能实现这个目标?

例如

ArrayList dataList = new ArrayList();

// dataList Add Item

out.println("<h1>" + dataList  +"</h1>"); // I want to view it in tabular format.

I have an ArrayList in my Servlet code. Now, I want to show that ArrayList in a tabular format. How can I achive this?

E.g.

ArrayList dataList = new ArrayList();

// dataList Add Item

out.println("<h1>" + dataList  +"</h1>"); // I want to view it in tabular format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

镜花水月 2025-01-09 09:05:46

表格采用 HTML 格式,由 元素表示。您应该使用 JSP 作为 HTML 代码来将视图与控制器分开(这将大大提高可维护性)。您可以使用 JSTL 来控制 JSP 中的流程。您可以使用 JSTL 标签来迭代集合。

在 servlet 中,将列表放入请求范围并转发到 JSP:

request.setAttribute("dataList", dataList);
request.getRequestDispatcher("/WEB-INF/dataList.jsp").forward(request, response);

/WEB-INF/dataList.jsp 中,您可以将其呈现在 HTML < /code> 如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${dataList}" var="dataItem">
        <tr>
            <td>${dataItem.someProperty}</td>
            <td>${dataItem.otherProperty}</td>
        </tr>
    </c:forEach>
</table>

另请参阅:

Tables are in HTML to be represented by <table> element. You should be using JSP for HTML code to separate view from the controller (which will greatly increase maintainability). You could use JSTL to control the flow in JSP. You can use JSTL <c:forEach> tag to iterate over a collection.

In the servlet, put the list in the request scope and forward to a JSP:

request.setAttribute("dataList", dataList);
request.getRequestDispatcher("/WEB-INF/dataList.jsp").forward(request, response);

In the /WEB-INF/dataList.jsp, you can present it in a HTML <table> as follows:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${dataList}" var="dataItem">
        <tr>
            <td>${dataItem.someProperty}</td>
            <td>${dataItem.otherProperty}</td>
        </tr>
    </c:forEach>
</table>

See also:

一梦浮鱼 2025-01-09 09:05:46

请参阅下面 ArrayList 的工作原理

out.println("<table>");
ArrayList dataList = new ArrayList();
// add some data in it....
for (Iterator iter = dataList.iterator(); iter.hasNext();) {
    out.println("<tr><td>" + (String)(iter.next()) + "</td></tr>");
}
out.println("</table>");

祝你好运!!!

See below how ArrayList works

out.println("<table>");
ArrayList dataList = new ArrayList();
// add some data in it....
for (Iterator iter = dataList.iterator(); iter.hasNext();) {
    out.println("<tr><td>" + (String)(iter.next()) + "</td></tr>");
}
out.println("</table>");

Good Luck!!!

遗失的美好 2025-01-09 09:05:46

您必须使用 HTML Tables 而不是 H1 元素,迭代对象列表并打印它们的属性

You would have to go with HTML Tables instead of H1 elements, iterate through the list of objects and print their properties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文