如何显示在表格形式的ArrayList? [英] How to display an ArrayList in tabular format?

查看:533
本文介绍了如何显示在表格形式的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ArrayList 在我的Servlet code。现在,我想证明的ArrayList 以表格格式。我怎样才能achive呢?

例如

 的ArrayList的DataList =新的ArrayList();// DataList控件添加项目通过out.println(&所述; H1>中+的DataList +&下; / H1>中); //我想以表格形式进行查​​看。


解决方案

表是在HTML中被重新$ P $由&LT psented;表> 元素。您应该使用 JSP 为HTML code分离从控制器视图(这将大大增加可维护性)。你可以使用 JSTL 控制在JSP中的流动。您可以使用JSTL &LT ; C:的forEach> 标签遍历集合

在servlet中,列入名单的请求范围,并转发给JSP:

 了request.setAttribute(DataList控件,DataList控件);
的request.getRequestDispatcher(/ WEB-INF / dataList.jsp)向前(请求,响应)。

/WEB-INF/dataList.jsp ,你可以present它在一个HTML <表> 如下:

<%@标签库的URI =htt​​p://java.sun.com/jsp/jstl/corepreFIX =C%GT;
...
<表>
    c为C:的forEach项目=$ {}的DataListVAR =DataItem的>
        &所述; TR>
            < TD> $ {dataItem.someProperty}< / TD>
            < TD> $ {dataItem.otherProperty}< / TD>
        < / TR>
    < / C:的forEach>
< /表>

参见:

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.

解决方案

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:

这篇关于如何显示在表格形式的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆