commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anil Mandava <>
Subject [PATCH] beanutils dynabean populating
Date Wed, 27 Mar 2002 17:00:28 GMT
I am trying to use the new DynaActionForm (from Struts beta1) with nested
properties, and it seems like that DynaActionForm ignores the nesting stuff.
While checking out code, it appears like BeanUtils.populate() does not seem
to traverse the property nesting. It works ok if I switch to regular form

I have attached a patch to jakarta-commons\ to take care of
nesting issue for dynabean properties.

I would very much appreciate if somebody can review and commit it if it is
appropriate fix.

Here are more details of the issue:

Form bean configuration from struts-config:
    <form-bean      name="cart" 
      <form-property name="name" type="java.lang.String"/>
      <form-property name="items" type="ShoppingItem[]"/>

MyDynaActionForm is an extension to prevent "reset()" from clearing "items"
MyFormBeanConfig is an extension to recognize MyDynaActionForm class name as
ShoppingItem is a bean with 3 properties - (String)id, (int)quantity, and
(boolean) remove.

Here is how my JSP looks:
<nested:form action="/updateCart">
<nested:text property="name"/>
  <nested:iterate id="it" property="items">
    <TD><nested:text property="id"/></TD>
    <TD><nested:text property="quantity"/></TD>
    <TD><nested:checkbox property="remove"/></TD>

<nested:submit property="Update" value="Update"/>

Generated html looks like (if I had two items in the shoppingItems array):
<form name="cart" method="POST" action="/cart/">
<input type="text" name="name">

    <TD><input type="text" name="items[0].id" value="first"></TD>
    <TD><input type="text" name="items[0].quantity" value="1"></TD>
    <TD><input type="checkbox" name="items[0].remove" value="on"></TD>
    <TD><input type="text" name="items[1].id" value="second"></TD>
    <TD><input type="text" name="items[1].quantity" value="2"></TD>
    <TD><input type="checkbox" name="items[1].remove" value="on"></TD>

<input type="submit" name="Update" value="Update">

So, when the form is submitted as is,
 BeanUtils has to populate "items[0].id" to "first" etc.

It invokes the equivalent of following on the dynaform bean.
set("items", 0, "first")  instead of get("items", 0).setId("first").

To be more precise, it invokes:
   PropertyUtils.setIndexedProperty(bean, //the dynabean
                                    dynaName, // "items"
                                    index, // 0
                                    newValue // "first"
It should really have done
  Object subbean = PropertyUtils.getIndexedProperty(bean, // the dynabean
                                                    dynaName, //"items"
                                                    index); //0
                                             dynaName, // "id"
Please let me know if I have identified the problem right, and if 
the patch seems like a resonable fix.

Thank you in advance,
Anil Mandava

View raw message