Thomas Moorer - 2008-01-17

Hi All,

I am trying to utilized index properties in a displaytag table, but I'm not sure how to go about it.

I have a series of reports to create that are pretty much cookie cutter. The only thing that varies is the column names. What I am trying to do is create a generic "Bean" class that is an array of names and and an array of values. I'm trying to display this in my table like

    <display:table requestURI="controller" name="sessionScope.searchResults" sort="external"  pagesize="<%= Integer.parseInt(recordsPerPage) %>" id="bean" partialList="true" size="sessionScope.resultsSize" class="com.hna.credit.beans.Bean" class="dstg" export="true">
        <%
            String[] columnNames = report.getColumnNames();
            String[] columnLabels = report.getColumnLabels();
            for (int i=0; i<columnNames.length; i++) {
        %>
            <display:column class="dstg" property="<%=((Bean)pageContext.getAttribute("bean")).getName(i)%>" sortable="true"><%=((Bean)pageContext.getAttribute("bean")).getValue(i)%></display:column>
        <%   
            }
        %>
    </display:table>

When I try to run this code, I get the following error:

Error looking up property "xxx" in object type "com.hna.credit.beans.Bean".

which makes me thing that DisplayTag is trying to do the normal property lookup by name, when I'm trying to return the value in the indexed property.

Here'e my Bean class if it will help.

public class Bean {

private String[] names;
private String[] values;

public Bean(String[] names) {
    this.names = names;
    this.values = new String[names.length];
}

public String getName(int index) {
    return names[index];
}

public void setValue(int index, String value) {
    this.values[index] = value;
}

public String getValue(int index) {
    return this.values[index];
}

public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("[Bean.java :");
    for (int i=0; i<names.length; i++) {
        if (i>0) {
            sb.append(",");
        }
        sb.append(" ");
        sb.append(names[i]);
        sb.append("='");
        sb.append(values[i]);
        sb.append("'");
    }
    sb.append("]");
    return sb.toString();
}

}