On Sat, 28 Jun 2003, Aaron Longwell wrote:
> Date: Sat, 28 Jun 2003 15:08:36 -0600 > From: Aaron Longwell <[EMAIL PROTECTED]> > Reply-To: Jakarta Commons Users List <[EMAIL PROTECTED]> > To: Commons User <[EMAIL PROTECTED]> > Subject: BeanUtils.copyProperties() Conversion Question > > I'm new to this list... and new to BeanUtils as well. Somebody pointed > out the BeanUtils.copyProperties method to me the other day... and WOW! > It's a great function.... just one issue: > > I'm using it in a struts application. On one edit form, I've got a > number of Integer fields. Some of these can have an empty (null) value. > When I populate the form, a null Business Object value converts to a > blank string on the input form (this is what I want). Now, when I save > that form back to the database, the empty string gets converted to a 0 > and stored in the database as a 0. Obviously, when the user edits the > same object again, now the form is populated with a 0. > > As a workaround, I am doing a manual check after the copyProperties > method runs and storing null when the source string was empty. > The standard answer is that your form bean should use String properties, not Integer properties. Besides dealing with the issue above, consider what happens when a user types "1a3" instead of "123" into a text field that is backed by an Integer: * If you are using a String property in your form bean, and a validation rule that checks for a valid integer (such as the "integer" rule in the Validator Framework), you get a nice error message and the user gets the chance to fix their mistake. * If you are using an Integer property in the form bean, Struts will throw an exception when trying to populate the form bean properties for you. Craig --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
