commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: BeanUtils.copyProperties() Conversion Question
Date Sat, 28 Jun 2003 23:59:00 GMT


On Sat, 28 Jun 2003, Aaron Longwell wrote:

> Date: Sat, 28 Jun 2003 15:08:36 -0600
> From: Aaron Longwell <listservs@newmedialogic.com>
> Reply-To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
> To: Commons User <commons-user@jakarta.apache.org>
> 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


Mime
View raw message