如何正确打印此表HTML / Java Web应用程序

时间:2012-03-01 10:33:59

标签: java html web-applications html-table

我无法尝试使用HTML打印表格,以便项目显示在正确的位置。目前我有这个。这会以一种列表打印数据,因此所有内容都在一个单独的列上。

                    <table border=0 width="75%">

                        <%
                                    for (int pos = 0; pos < list.size(); pos++) {
                                        Menu menu = list.getMenuAt(pos);
                        %>

                        <tr>
                          <tr width="80%">
                            <b>Title:</b><br>
                            <b>Dish:</b><br>
                            <b>Description:</b><br>
                            <b>Price:</b><br><br>
                          </tr>
                        </tr>
                        <tr>
                            <td width="80%">
                                <%= menu.getTitle()%> <br>
                                <%= menu.getDish()%> <br>
                                <%= menu.getDescription()%> <br>
                                <%= menu.getPrice()%> <br><br>
                            </td>
                        </tr>

                        <%
                                    } // end for
                        %>

                    </table>

菜单上可以有一个标题,上面有很多菜名,菜品描述和价格。

2 个答案:

答案 0 :(得分:1)

<tr>内嵌有<tr><td>。这些是表格行。将表格单元格更改为<tr> <td width="80%"> <strong>Title:</strong><br/> <strong>Dish:</strong><br/> <strong>Description:</strong><br/> <strong>Price:</strong><br><br/> </td> </tr>

<strong>

另外,使用HTML 4中的<br/>并关闭<table border="0" width="75%"> <!-- Table header --> <tr> <td><strong>Title:</strong></td> <td><strong>Dish:</strong></td> <td><strong>Description:</strong></td> <td><strong>Price:</strong></td> </tr> <% for (int pos = 0; pos < list.size(); pos++) { Menu menu = list.getMenuAt(pos); %> <!-- Table contents --> <tr> <td><%= menu.getTitle(); %></td> <td><%= menu.getDish(); %></td> <td><%= menu.getDescription(); %></td> <td><%= menu.getPrice(); %></td> </tr> <% } // end for %> </table> 标记,但这些标记大多是迂腐的:)

编辑:

查看您的代码,我认为您想要的是标题行,然后执行for循环并使用单个&#34;菜单填充每一行&#34; for循环中每个对象的项目。试试这个:

{{1}}

您只需要标题为&#34;标题&#34;,&#34; Dish&#34;等一旦上到顶部,然后运行for循环并在之后添加行。请注意,每行都有一个单独的单元格上的字段,这是您应该如何分隔表格数据。我建议使用CSS进行样式设计,最好是包含一个外部CSS文件。

答案 1 :(得分:1)

我不明白你为什么在<br/>元素中有<td>。您的代码将以下列内容分隔每个字段:

Title:
Dish:
Description:
Price:

text
text
text
text

我认为你的代码应该是这样的:

<table border=0 width="75%">

<thead>
    <tr>
        <th>Title</th>
        <th>Dish</th>
        <th>Description</th>
        <th>Price</th>
    </tr>
</thead>
<%
            for (int pos = 0; pos < list.size(); pos++) {
                Menu menu = list.getMenuAt(pos);
%>

<tbody>
    <tr>
        <td><%= menu.getTitle()%></td>
        <td><%= menu.getDish()%></td>
        <td><%= menu.getDescription()%></td>
        <td><%= menu.getPrice()%></td>
     </tr>
</tbody>

<%
            } // end for
%>