If I have a actionForm that contains a field that is type Integer, how do I
get the form submition to submit null rather than 0?
ie
<html:select property="myInteger">
<option></option>
<option value="0">0</option>
</html:select>
both set the value as 0, instead of the first being null.
Ryan Dillon - 23 Apr 2005 12:21 GMT
This behavior is due to the Struts Converters class. By default it
registers a Converter for Integers that converts null String values
into 0. So, if you really wanted the form field to be an Integer, you
could register a new converter, something like:
ConvertUtils.register(new IntegerConverter(null), Integer.TYPE);
ConvertUtils.register(new IntegerConverter(null), Integer.class);
Generally though it is better to use String properties on forms.
Consider in your case with the Integer property, that the Javascript
validation fails (maybe the user has disabled Javascript?) and the user
has entered "one", this will be converted to new Integer(0) *before* it
is validated on the server, and it would then pass validation. If the
property had been a String it would be validated properly on the server
and the user would see the validation error. The downside of using
String form properties is that you must then parse the String value
into an Integer when you need to use it.
Regards
--
Ryan Dillon
Code Canvas Technologies
RapidJ - Rapid Java Web Development
http://www.codecanvas.com.au/rapidj/
Wendy S - 24 Apr 2005 03:48 GMT
> If I have a actionForm that contains a field that is type Integer, how do
> I get the form submition to submit null rather than 0?
Best practices say to use Strings in your form. Struts uses (Jakarta
Commons) BeanUtils behind the scenes, and you can use
BeanUtils.copyProperties(...) to copy values from the form into your actual
value object/DTO.
If you find that the automatic conversion isn't what you want, you can write
and register a Converter with BeanUtils.

Signature
Wendy Smoak