This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes I am getting 0 for an Integer property if user dont give any values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "I am getting 0 for an Integer property if user dont give any values" Watch "I am getting 0 for an Integer property if user dont give any values" New topic
Author

I am getting 0 for an Integer property if user dont give any values

Gnanasekaran Sakthivel
Greenhorn

Joined: Jul 14, 2004
Posts: 23
JSP page [ Using Struts 1.1 ]


FormBean



In the form bean, I am getting Value 0 if the user leaves the text fields empty since it is not required.

Is there any possibility we can get null only if the user does not give any values.

Thank you for your help.
This issue is been bothering me for a long time


Best Wishes
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Correct me if I'm wrong here, but isn't the null value of an Integer 0?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Chris is exactly right. There's no way to put a null value in an integer. Even if you use the wrapper class, Struts is still going to interpret a blank as being zero. I'd suggest changing the property to a String. Then you can test for a null string in order to know whether the user entered a value or not.

You can then use validation logic to make sure the value entered is an integer. Once the value has been validated, you can safely convert it to an integer in your model logic.


Merrill
Consultant, Sima Solutions
Gnanasekaran Sakthivel
Greenhorn

Joined: Jul 14, 2004
Posts: 23
I found out and think Struts behind the scences using the
org.apache.commons.beanutils.ConvertUtilsBean.ConvertUtilsBean in the process of populating the values from HTTPRequest to form bean properties.

Refer this page for further info
http://jakarta.apache.org/commons/beanutils/apidocs/org/apache/commons/beanutils/ConvertUtilsBean.html

Here it says for Integer Form-Bean property, it defaults to 0.
int and java.lang.Integer (default to zero)

So it defaults to 0.

Do anyone have any quick solution. I would like to differentiate between the value 0 and null.

Thank you
Gnanasekaran Sakthivel
Greenhorn

Joined: Jul 14, 2004
Posts: 23
Thank you Merrill and Chris, can you update me with my latest reply.

You know we have in our project (99%) most of the places String properties, then we do validate. In few places we have done some mess, so keeping it String and doing validation is a pain.
Can I extend the ConversionUtil class or do something, or can I configure the conversionUtil class using struts configuration.

Thanks again for your reply.
Gnanasekaran Sakthivel
Greenhorn

Joined: Jul 14, 2004
Posts: 23
Guys

did you look into the page that I refered above.

It says the following.

Converters generally treat null input as if it were invalid input, ie they return their default value if one was specified when the converter was constructed, and throw an exception otherwise. If you prefer nulls to be preserved for converters that are converting to objects (not primitives) then register a converter as above, passing a default value of null to the converter constructor (and of course registering that converter only for the .class target).


Can you give some comments on this
Thank you again
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
My point of view is that if you're going to use a framework such as Struts, it's not advantageous to fight against the way the framwork functions. Struts was designed to use Strings in ActionForm classes. Why fight it?

Even if you do create your own converter and register it with apache BeanUtils, you are still going to have the problem of validation. If the user enters something other than a valid integer, BeanUtils is going to throw a NumberFormatException. Furthermore, when the page is redisplayed, it is not going to show the user the invalid data, but rather a zero or null value.

Even in light of what you have discovered, my advice is still to use a String instead of an integer in your ActoinForm.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
The recommended way to handle this is to declare your form properties as Strings. If zero is a valid value for the user to enter, then it seems like you should change your form to use Strings. Another issue with using an Integer is that if the user enters an invalid entry like "25 ft" you will not be able to perform validation (except for the field being required) because this value cannot be converted to an Integer so it will be zero by the time validation is performed.

There is a setting you can made in web.xml that will cause empty values to translate to nulls in your form, but this setting applies to all actions. In my Struts book it is called convertHack but at some point it was renamed to convertNull.

- Brent
Gnanasekaran Sakthivel
Greenhorn

Joined: Jul 14, 2004
Posts: 23
Thank you so much Guys. I am going to do String only which I was following all along.
 
jQuery in Action, 2nd edition
 
subject: I am getting 0 for an Integer property if user dont give any values
 
Similar Threads
problem in DynaValidatorForm
problem in DynaValidationForm
Javascript and Struts Indexed Text Boxes
Accessing an element in a List
Retriving values from html:checkbox in a jsp