在Java中创建HTML表
当我使用HTML在Java中创建表时,我的代码中有一个问题。这是我的代码:在Java中创建HTML表,java,html,Java,Html,当我使用HTML在Java中创建表时,我的代码中有一个问题。这是我的代码: for(int station : stations){ String rowcolor = null; String stationnum = Integer.toString(station); String lastDate = pollData(station); //CALL GET LAST String status = determineStatus(station, lastD
for(int station : stations){
String rowcolor = null;
String stationnum = Integer.toString(station);
String lastDate = pollData(station); //CALL GET LAST
String status = determineStatus(station, lastDate); // CALL DETERMINE STATUS
switch(status){
case " ONLINE":
rowcolor = (" <tr bgcolor=\"#5FFF33\">");
break;
case " OFFLINE":
rowcolor = (" <tr bgcolor=\"red\">");
break;
case " DELAYED":
rowcolor = (" <tr bgcolor=\"yellow\">");
break;
}
out.write("<html>" +
"<body>" +
"<table border ='1'>" +
"<tr>" +
"<td>Station Number</td>" +
"<td>Station Name</td>" +
"<td>Status</td>" +
"<td>As of Date</td>" +
"</tr>");
out.write(rowcolor + "<td>");
out.write(stationnum);
out.write("</td><td>");
out.write(stationnname[id]);
out.write("</td><td>");
out.write(status);
out.write("</td><td>");
out.write(lastDate);
out.write("</table>" +
"</body>" +
"</html>");
id++;
out.close();
}
}catch (IOException e) {
System.err.println(e);
}
用于(内部站点:站点){
字符串rowcolor=null;
字符串stationnum=Integer.toString(station);
String lastDate=pollData(station);//调用GET LAST
String status=determineStatus(station,lastDate);//调用determinate status
开关(状态){
“在线”案例:
rowcolor=(“”);
打破
案例“离线”:
rowcolor=(“”);
打破
“延迟”案件:
rowcolor=(“”);
打破
}
请写出(“”)+
"" +
"" +
"" +
“车站号码”+
“电台名称”+
“地位”+
“截至日期”+
"");
out.write(rowcolor+“”);
out.write(stationnum);
请写出(“”);
out.write(stationnname[id]);
请写出(“”);
输出。写入(状态);
请写出(“”);
填写(最后日期);
请写出(“”)+
"" +
"");
id++;
out.close();
}
}捕获(IOE异常){
系统错误println(e);
}
这是输出:
当我移除out.close()时代码>零件,输出如下:
如您所见,图像显示在创建表时存在问题。有些地方不对劲,但我找不到解决的办法。请帮帮我;提前谢谢。正如菲尔所说,out.close()
位于for
循环内,您需要将其更改为for
循环外,因为如果它位于循环内,out
将在第一次迭代时关闭,而对其他记录不起作用
for(int station : stations){
}
out.close();
查看您正在向输出缓冲区写入的内容和位置
在for
循环中,您正在编写一个完整的HTML文档(即…
)和一个包含标题行和一个数据行的整个表
我假设您要做的是继续将表行写入一个表。为此,将上述标记写入循环的外部
out.write("<html><body><table border=\"1\"><thead>" +
"<tr><td>Station Number</td><td>Station Name</td>" +
"<td>Status</td><td>As of Date</td></tr></thead><tbody>");
for(int station : stations) {
// get data, determine rowcolor, etc
out.write(rowcolor + ... + "</tr>");
}
out.write("</tbody></table></body></html>");
out.close();
out.write(“)+
“站点编号站点名称”+
“截至日期的状态”);
用于(内部站点:站点){
//获取数据、确定行颜色等
out.write(rowcolor+…+);
}
请写出(“”);
out.close();
将生成的HTML视为源代码。我想你会很快发现问题所在,我想像图一一样控制桌子,但它停止了。所以当我移除out.close()时;部分,它将获得所有的电台,但它显示它一个接一个…是的,我知道你想做什么。你看过生成的HTML源代码了吗?它不是循环中唯一不应该看到的东西there@Phil,是的,这不是唯一的问题,但这个问题是最严重的,我把放出来了。close()代码>外部零件,但输出仍与图像中相同2@Phill我听从了你的指示,现在它对我起了作用。非常感谢。