Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

stop setting ints to 0

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you stop struts setting int fields to 0 if nothing has been entered in the field - i want nothing to mean null not 0
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
What would you expect Struts to set the value to? null isn't a valid value for an int, so it has to be something.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally, it's best to make properties on an ActionForm either Strings or booleans. If you want to make sure the value could always be converted to an int, you can do so through validation. If you're getting the value as an int from some external source, just convert it to a String when populating your ActionForm.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use the Integer class instead of primative ints, the convertNull initialization parameter of the ActionServlet when set to true will initialize the Integers to null.

I'd go along with Merrill's suggestion though.

Oh, and before anyone mentions it, I haven't been here for years and am aware of the naming policy now in force. I'll change it.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be a good entry for the FAQ. I see this question come up often. While playing around with WebWork a few weeks ago I noticed that pages seemed to work fine using data types such as Integer.

There are two reasons that I use Strings for all editable fields. The first mentioned above is that it gives you the ability to tell if the user has left the field blank or entered a zero value (I have not tried the "convertNull initialization parameter", but that would be another way).

The second reason is for validation. Say your form has a Distance field that you expect to be a number but the user enters "Ten Miles". The Struts framework will try to populate your form's Integer property, but since it cannot convert this string into an Integer it will ignore the user's value. By the time sever side validation is performed, the user's value will be lost, so you cannot even tell the user that they entered an invalid number. Also, if the page hits another validation error then the page will be redisplayed with values from the Form and the Distance field will be empty.

Okay, a third reason...If you do not do your own conversion of numbers to strings then you have to rely on the default formatting of the toString method. That works okay most of the time but you may end up with values such as "3.9999999997" and large numbers are formatted using scientific notation.

I do use types such as int for fields that the user cannot edit such as hidden fields, and list selection value.

- Brent
[ July 26, 2006: Message edited by: Brent Sterling ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic