Telegard
Telegard

Reputation: 137

JSP/JSTL parsing ArrayList that is property of passed object

I am trying to sort through a tricky JSTL problem, hoping to get some help with...

I am passing a table object from Spring MVC controller to a JSP using ModelAndView, the table object has an ArrayList of Row objects, which in turn each have an ArrayList of Field objects.

I am populating the Table object in the controller for display on the jsp page, and I am trying to figure out how to parse through the Arraylists to produce an html table using JSTL.

So as a starting point, looking at the Table object...

Using Spring 3.0 MVC

Model:

public class Table {

private String id;
private ArrayList<Row> rows;

public String getId() {
    return id;
}

public ArrayList<Row> getRows() {
    return rows;
}

public void setRows(ArrayList<Row> rows) {
    this.rows = rows;
}

Controller:

Table table = new Table();
ArrayList<Row> rows = new ArrayList<Row>();
ArrayList<Field> fields = new ArrayList<Field>();

fields.add(new Field("0","test0"));
fields.add(new Field("1","test1"));
fields.add(new Field("2","test2"));

Row row = new Row();
row.setPKValue("testPK");
row.setFields(fields);
rows.add(row);

table.setRows(rows);
table.setId("test");

ModelAndView mav = new ModelAndView();
mav.addObject("table",table);
mav.setViewName("healthcenter/report");

return mav;

JSP File:

<c:forEach var="row" items="${table.rows}">
${row.PKValue}
</c:forEach>

This doesn't produce any output, I was expecting the row PKValue property to be output, not sure where the problem is, any insight would be great!

Upvotes: 0

Views: 3694

Answers (1)

skaffman
skaffman

Reputation: 403491

Assuming the method on Row is getPKValue, then you need to refer to it in the JSP as pKValue, not PKValue, i.e.

${row.pKValue}

It's a poor name for the property, though, it looks confusing. I suggest renaming it to something more conventional, e.g. getId (i.e. ${row.id}) or getKey (i.e. ${row.key}).

Upvotes: 1

Related Questions