如何正确打印此表HTML/Java web app
我在试图用HTML打印表格以使项目显示在正确的位置时遇到问题。目前我有这个。这将以列表的形式打印数据,以便所有内容都位于单独的列中如何正确打印此表HTML/Java web app,java,html,web-applications,html-table,Java,Html,Web Applications,Html Table,我在试图用HTML打印表格以使项目显示在正确的位置时遇到问题。目前我有这个。这将以列表的形式打印数据,以便所有内容都位于单独的列中 <table border=0 width="75%"> <% for (int pos = 0; pos < list.size(); pos++) {
<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>
标题:
菜肴:
说明:
价格:
菜单可以有一个标题,其中包含许多菜名、菜名和价格。您不能将
嵌套在
中。这些是表格行。对于表格单元格,将其更改为
<tr>
<td width="80%">
<strong>Title:</strong><br/>
<strong>Dish:</strong><br/>
<strong>Description:</strong><br/>
<strong>Price:</strong><br><br/>
</td>
</tr>
标题:
菜肴:
说明:
价格:
另外,从HTML4中使用
,并关闭
标记,但这些标记大多是迂腐的:)
编辑:
看看你的代码,我认为你想要的是一个标题行,然后做一个for循环,并为for循环中的每个对象在每一行中填充一个“菜单”项。试试这个:
<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>
标题:
菜肴:
说明:
价格:
您只需要标题中有“Title”、“Dish”等,然后运行for循环并在其后添加行。请注意,每一行都有一个单独的单元格上的字段,这是分隔表格数据的方式。我建议使用CSS进行样式设置,最好包括一个外部CSS文件。我不明白为什么在
元素中有
。您的代码将以如下方式分隔每个文件:
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
%>
我认为您的代码应该是这样的:
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
%>
标题
盘子
描述
价格
您可能需要提供更多关于表的外观的信息。请将数据处理与视图分开?请参阅并在for循环开始之前移动表格标题,我的猜测是您不希望重复标题,同时也意识到他们没有将每个字段设置为单独的字段,这将使所有内容都不对齐