You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Jouravlev <jm...@gmail.com> on 2006/03/03 22:23:13 UTC

Cannot set value of a nested bean

This question actually is more related to commons and BeanUtils, but I
ask it here first.

So, I have an ActionForm with a property:


private Part part;
public Part getPart() {return part;}
public void setPart(Part part) {this.part = part;}


Part contains part name field:


public MyProperty myPartName = new MyProperty() {
    String partName;
    public Object getValue() {return partName;}
    public void setValue(Object value) {
        if (value != null) {
            partName = (String) value;
        }
    }
    ...
};
public MyProperty getPartName() {return myPartName;}


where MyProperty is defines as:


public abstract class MyProperty {
    public abstract Object getValue();
    public abstract void setValue(Object value);
    public abstract void validate();
}


Then I try to access the fields of MyProperty, for example:


<html:form ...>
  Part Name: <html:text name="crudform" property="part.partName.value"/>
</html:form>


This works perfectly for read access, that is, getter works. But when
I try to set it, I get this exception:

Caused by: java.lang.NoSuchMethodException: Property 'value' has no
setter method
	at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1760)
	at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1662)
	at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1691)
	at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1026)
	... 38 more

I looked in the source code for BeanUtilsBean and PropertyUtilsBean.
There is a method PropertyUtilsBean.getWriteMethod(PropertyDescriptor
descriptor) that verifies if a certain method is accessible. It calls
MethodUtils.getAccessibleMethod(descriptor.getWriteMethod()), which in
turn calls getAccessibleMethodFromInterfaceNest() and the latter
returns null.

Method descriptor.getWriteMethod() returns proper "setValue" method
name, but if I comment out the call to
MethodUtils.getAccessibleMethod() and just return the method name from
descriptor, I get reflection error:

java.lang.IllegalAccessException: Class
org.apache.commons.beanutils.PropertyUtilsBean can not access a member
of class net.jspcontrols.dialogs.samples.crud.business.Part$1 with
modifiers "public"
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
	at java.lang.reflect.Method.invoke(Method.java:317)
	at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1788)
        ...

How do I make the setter work?

Thanks,
   Michael.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org