tags:

views:

81

answers:

2

Hi.

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 data 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.

Thanks in advance.

A: 

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.

leonbloy
A: 

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>
BalusC
Thanks for your complete answer.
Zim