cod*_*247 1 html jsp html-table dynamic
我将使用表中配置的值创建动态html表.我正在使用JSP和MVC架构.
我在表格中有行号,列号和值字段.如果值相应地为1.5和HELLO,那么我将在第1行和第5列中显示此HELLO.
表结构如下所示.
row column value
1 5 value1
2 8 value2
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
您需要先收集数据String[][]:
int rows = getMaxRowNumberFromDB();
int cols = getMaxColNumberFromDB();
String[][] values = new String[rows][cols];
// ...
while (resultSet.next()) {
int row = resultSet.getInt("row");
int col = resultSet.getInt("col");
String value = resultSet.getString("value");
values[row][col] = value;
}
Run Code Online (Sandbox Code Playgroud)
(注意:根据该数组索引是从零开始,你可能想从1.减去row和col第一)
然后在JSP中使用JSTL c:forEach(或"MVC架构"正在使用的任何数据迭代标记)显示它:
<table>
<c:forEach items="${values}" var="row">
<tr>
<c:forEach items="${row}" var="value">
<td>${value}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
Run Code Online (Sandbox Code Playgroud)