This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "IntegerConverter converts empty string to 0" Watch "IntegerConverter converts empty string to 0" New topic

IntegerConverter converts empty string to 0

Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
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.

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.

Consultant, Sima Solutions
I agree. Here's the link:
subject: IntegerConverter converts empty string to 0
jQuery in Action, 3rd edition