<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> <!-- data-ad-client=ca-pub-4320963827702032 --> <!-- --><style type="text/css">@import url(https://www.blogger.com/static/v1/v-css/navbar/3334278262-classic.css); div.b-mobile {display:none;} </style> </head><body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d7256432\x26blogName\x3dThe+Frustrated+Programmer\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLACK\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://frustratedprogrammer.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://frustratedprogrammer.blogspot.com/\x26vt\x3d4213664491834773269', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe", messageHandlersFilter: gapi.iframes.CROSS_ORIGIN_IFRAMES_FILTER, messageHandlers: { 'blogger-ping': function() {} } }); } }); </script>
| Wednesday, June 09, 2004

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.