Reputation: 715
I want to have a fixed table structure on my jsp page (3row, 4column). but I want to load data for this table from DataBase with using struts 2. I know if my table structure wasn't fixed I could have just get a List and iterate on it and add a <tr><td>data</td></tr>
in every iteration, but how could I do this in this case.
also if I don't have enough data to fill table, I want those places to be empty.
I didn't find a good example, if you could help me or introduce me a good tutorial, it would be really appreciated.
Upvotes: 1
Views: 2780
Reputation: 1108712
You need to collect the data in a String[][]
or List<List<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; // Note: array index is zero based!
}
Since I don't do Struts2, here's a JSTL example to get the picture how you could do it with a similar Struts2 tag:
<table>
<c:forEach items="${values}" var="row">
<tr>
<c:forEach items="${row}" var="value">
<td>${value}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
Upvotes: 1
Reputation: 75906
It's not very clear for me. Typically (and conventionally) table listings have a fixed number of columns and variable number of rows (say, as in a SQL select). If that's your scenario, with the only difference that you want to impose a fixed number of rows, you can code that restriction into your (say) getData()
action method, so that it always return a list with three elements (you'd take care of filling absent rows with some dummy-empty data).
Or perhaps the three rows represent different kind of data. Then, you have actually a matrix of 3x4 elements. But then you could code a general Object getMatrixData(int i,int j)
method, or even a particular method for each cell.
Upvotes: 0