Search code examples
javajspjdbcstruts2ognl

Iterating through 2D ArrayList and displaying it on JSP page


I tried looking this up but didn't find anything that answered my question.

So what I've got is something that looks like this:

private List<List<String>> data = new ArrayList<List<String>>();

This has a getter / setter and is being populated by this line:

String[] name = new String[columnCount];
for (int i = 0; i < columnCount; i++ ) {
  name[i] = rsmd.getColumnName(i+1);
  Array tempArray = rs.getArray(name[i]);
  data.add((List<String>) tempArray);
}

In my jsp I know I need something like this:

<s:iterator value="data">
        <th><s:property/></th>
    </s:iterator>
</tr>

I'm stuck though on how to iterate a 2D array and if it matters that it's an ArrayList.


Solution

  • Array is a SQL type, you need to get a Java array to use with the iteratator tag.

    data.add((List<String>) Arrays.asList(tempArray.getArray()));
    

    On JSP you should use two nested iterator tags.

    <s:iterator var="row" value="data">
      <tr>
      <s:iterator value="#row">
        <td><s:property/></td>
      </s:iterator>
      </tr>
    </s:iterator>