commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brian Bonner" <>
Subject Re: BeanUtils.copyProperties() Conversion Question
Date Sun, 29 Jun 2003 15:42:21 GMT
So your business Value object is using an Integer class as opposed to a 
primitive (int)?  You could register a new converter that provides any 
kind of behavior you are interested in.  Do you want it to behave like:

Form       DB
""    <-->   null
Valid int String  <-->  Numeric


On Sun, 29 Jun 2003 9:47AM -0500, Aaron Longwell wrote:
> I don't think I was very clear...
> the form bean currently contains only String fields.... the field in 
> question is a String, but needs to be restricted to an Integer value. I 
> am using the Validator framework to take care of this.
> The problem I'm having is that a String value of  "" (zero-length) in 
> the form bean is getting turned into a 0 in the database (instead of a 
> null, as I'd like to have happen in that scenario.
> Thanks,
> Aaron
> Craig R. McClanahan wrote:
>> On Sat, 28 Jun 2003, Aaron Longwell wrote:
>>> Date: Sat, 28 Jun 2003 15:08:36 -0600
>>> From: Aaron Longwell <>
>>> Reply-To: Jakarta Commons Users List <>
>>> To: Commons User <>
>>> 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:
>> For additional commands, e-mail:
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

View raw message