Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IntegerConverter converts empty string to 0

 
Tauqueer Ali
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic