问题描述
我正在尝试遍历2d整数数组,并使用<table>
标记在网格中表示它们.问题是我不允许使用任何Java脚本.我知道这是下面代码的作用,但更复杂.并且boardArray
返回2d整数数组.那么我将如何提取每个单元格的值?数组也有预定大小.
I am trying to iterate through a 2d array of integers and represent them in a grid using the <table>
tag. The catch is that I am not allowed to use any java-script. I know it is something to the effect of the below code, but more complicated. And boardArray
returns a 2d integer array. So how would I extract the value at each cell? There is a predetermined size of the array as well.
<c:forEach var="array" items="${bean.boardArray}">
<tr>
<td>${print out contents of a row}</td>
</tr>
</c:forEach>
推荐答案
使用纯HTML不能做到这一点.您在原始问题标题中提到了HTML,但是由于您已经附加了javabeans
标记并提到了c:forEach
标记,所以我想您的意思是JSP和JSTL而不是HTML.
在这里,JSP + JSTL解决方案的格式设置为易于阅读. Bean代码:
You can't do that with plain HTML. You've mentioned HTML in the original question title, but since you've attached the javabeans
tag and mentioned the c:forEach
tag, I suppose you mean JSP and JSTL instead of HTML.
Here the JSP+JSTL solution, formatted to be better readable. The bean code:
package com;
public class TransferBean {
private int[][] _boardArray = {
{ 1, 2, 33, 0, 7},
{ 13, 11, 7, 5, 3},
{ 5, 3, 2, 1, 1},
};
public int[][] getBoardArray() {
return _boardArray;
}
public int getBoardArrayRowLength() {
if (_boardArray == null || _boardArray.length == 0
|| _boardArray[0] == null) {
return 0;
}
return _boardArray[0].length;
}
}
这里是JSP文件的内容:
Here the JSP file content:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="bean" class="com.TransferBean" />
<table>
<thead>
<tr>
<c:forEach var="i" begin="1" end="${bean.boardArrayRowLength}">
<th>Column ${i}</th>
</c:forEach>
</tr>
</thead>
<tbody>
<c:forEach var="row" items="${bean.boardArray}">
<tr>
<c:forEach var="column" items="${row}">
<td>
${column}
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
数组内容由两个嵌套的c:forEach
循环呈现.外循环遍历行,对于每一行,嵌套循环遍历给定行中的列.
The array content is rendered by two nested c:forEach
loops. The outer loop iterates over the rows, and for each row, the nested loop iterates over the columns in the given row.
上面的示例在浏览器中如下所示:
Above example looks like this in a browser:
这篇关于需要使用< c:forEach>的帮助.在JSP/JSTL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!