Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

stop setting ints to 0

 
rob harvey
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • 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.
 
Ravi Mann
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic