Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Struts form submition with Integer

Thread view: 
Scott Phelps - 22 Apr 2005 22:41 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?

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.