Hi everyone. I've looked through the archives for this and the commons list,
without finding an answer to my problems with receiving indexed properties
from a form.

I've tried with both 1.1 beta and a nightly build from a couple of nights
ago. I'm not sure if my problem is with Struts, Commons / BeanUtils, or my
understanding of how indexed properties on a bean work.

I have a form which, amongst other things, has a tabular set of data:

...
<input type="hidden" name="rows[0].id" value="ROW1-PAINT">
<input type="hidden" name="rows[0].name" value="Paint colour">
    <td></td>
    <td>Paint colour</td>
    <td><input type="text" name="rows[0].value" value="Blue"></td>
    <td></td>
  </tr>
  ...

In my form bean I have represented this with an indexed property.

    ...
    public Row [] getRows() {
        if (rows == null)
            setRows(new Row [0]);

        return rows;
    }
        
    public void setRows(Row [] rows) {
        this.rows = rows;
    }
    ...

Where Row is an inner (nested? never sure of the terminology - defined as
public static inside the form bean, anyway).

My problem is when Struts comes to populate my form bean with values from
the form. Instead of creating a new Row [] and calling setRows, it calls
getRows and then attempts to set values within the array. Trouble is, the
array isn't big enough, leading to an ArrayIndexOutOfBoundsException. And
there's no way I can tell how big to make the array, as the size of the form
varies.

So, I tried using a List instead of an array. But with the same result -
Struts would grab the list from public List getRows(), and then attempt to
set values at indexes within the list.

So, in desperation, I went back to my array version and added a getter and
setter that would take an index, but would 'resize' the array when the index
would be out of bounds:

    ...
    public Row getRows(int i) {
        if (i >= getRows().length)
            increaseRows(i + 1);
        
        return getRows()[i];
    }
        
    public void setRows(int i, Row row) {
        if (i >= getRows().length)
            increaseRows(i + 1);
        
        getRows()[i] = row;
    }
    ...

Now, that works. But I'm not very satisfied. I would have thought that, in
my original code, the only proper way to change values of the property would
be to create a new array and pass it to public void setRows(Row [] rows).
One argument for this is (thinking only of JavaBeans, not Struts) if the
property was bound, and the array was changed behind the objects back, it
wouldn't know to fire a PropertyChangeEvent.

So, is this a bug in Struts / Commons-BeanUtils? Or a flaw in my
understanding of JavaBeans? Or just a limitation I have to live with? Is the
workaround above 'legal' in terms of the JavaBeans spec? Is there a nicer
way to achieve what I'm trying to do?

Thanks in advance...

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to