my dog learned polymorphism*
The moose likes Struts and the fly likes IntegerConverter converts empty string to 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "IntegerConverter converts empty string to 0" Watch "IntegerConverter converts empty string to 0" New topic
Author

IntegerConverter converts empty string to 0

Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
Hi,
We have a struts application which was recently migrated to 1.2.8 from 1.0. ActionForms in our app have value objects that contain Integer objects. These are getting initialized to 0 by struts framework at the time of submitting a form. org.apache.commons.beanutils.converters.IntegerConverter seems to be the culprit. We want the un-initialized Integer objects to remain null. Has anybody encountered a similar issue and do we have a workaround available. It seem custom converters can do the job for us. However, when beantutils are separetely used then it can be easily done, but when it's called from struts, then it may not be that easy. I need your help in sorting out this issue.

Thanks a lot.
Tauqueer.


SCJP2, SCWCD, XML, OOAD<br />Kolkata, India
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
One solution would be to create a second getter/setter pair for the property (e.g. getQtyAsString/setQtyAsString) that treat it as a String and then convert it to and from an integer. Then in your JSP, you'd write <html:text property="qtyAsString" />. That way struts will treat it as a String and not try to put zeroes into it. If you look at the documentation, the authors of Struts have always urged us to use Strings as properties in ActionForm beans.


Merrill
Consultant, Sima Solutions
 
jQuery in Action, 2nd edition
 
subject: IntegerConverter converts empty string to 0
 
Similar Threads
Three Table/Class Combination Display?
'==' operators with Wrapper class
ArrayList, clone, and primitive boxed values: shallow copy acting like a deep one!
Boxing,autoboxing,unboxing
Compile fails (generics/collections)