This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Struts and the fly likes stop setting ints to 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "stop setting ints to 0" Watch "stop setting ints to 0" New topic
Author

stop setting ints to 0

rob harvey
Greenhorn

Joined: Apr 05, 2002
Posts: 25
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Ravi Mann
Greenhorn

Joined: Jun 29, 2001
Posts: 8
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

Joined: Feb 08, 2006
Posts: 948
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stop setting ints to 0