These beans seem less and less cool every day (Yeah, I know I'm the only one on the planet that is still using them). Today I get the following error:
org.apache.commons.beanutils.ConversionException: Cannot assign value of type 'java.math.BigDecimal' to property 'foo' of type 'java.lang.String'
org.apache.struts.action.DynaActionForm.set(DynaActionForm.java:423)
org.apache.commons.beanutils.PropertyUtils.copyProperties(PropertyUtils.java:314)
Now, what's happening is I'm calling PropertyUtils.copyProperties() trying to prepopulate my form bean with data from my business object. There is a type mismatch between foo in the business object that is a BigDecimal and foo in the form which is a String. This is because in struts-land, form beans tied to a text input MUST always be of type String because first struts populates, then it validates.
Struts/Validator/BeanUtils are no help here, but the fix is fairly easy I think. Just extend DynaActionForm add override set() as follows:
public void set(String name, Object value) {
DynaProperty descriptor = getDynaProperty(name);
if (value != null) {
if (descriptor.getType() != null && descriptor.getType().getName().equals(String.class.getName())) {
super.set(name, value.toString());
} else {
super.set(name, value);
}
}
}
Well, this is just half the problem. This does not fix the post population of the business object. The error here is in PropertyUtils. PropertyUtils IMO needs some pluggable data conversion utilities. It just throws Conversion Exceptions now.
<< Home