posted 18 years ago
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 ]