I have a 2D array in java, trying to print it in table form in jsp. Below code is working -
<table>
<c:forEach items="${combinations}" var="face">
<tr>
<td>${face[0]}</td>
<td>${face[1]}</td>
<td>${face[2]}</td>
</tr>
</c:forEach>
</table>
But instead of hard coding index, I need to make it dynamic based on size of array. Please help with this.
CodePudding user response:
You can use for lops the same way as in plain java like this:
<table>
<%
int[][] arr = { {0,1,2}, {3,4,5}, {6,7,8}};
for (int rowIndex = 0; rowIndex < arr.length; rowIndex ) {
%><tr><%
for (int numIndex = 0; numIndex < arr[rowIndex].length; numIndex ) {
%><td><%= arr[rowIndex][numIndex] %></td><%
}
%></tr><%
}
%>
</table>
You just have to make sure that every element of your code is inside a <% ... %>
statement.
The outhput of this code looks like this:
In JSF it could look like this:
<c:forEach items="${combinations}" var="rows">
<tr>
<c:forEach items="${rows}" var="face">
<td>${face}</td>
</c:forEach>
</tr>
</c:forEach>