commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aaron Longwell <listse...@newmedialogic.com>
Subject Re: BeanUtils.copyProperties() Conversion Question
Date Mon, 30 Jun 2003 15:05:26 GMT
Brian,

That's correct:

Business Object:
private Integer expectedAttendance = null;

ActionForm
private String expectedAttendance = null;

Database
ExpectedAttendance default null

The current conversion (ActionForm on left):
""  --> 0
null --> 0

(I'm not sure which of those actually happens in my instance. The string 
is initialized to null... but upon the POST, the ActionForm gets 
populated with HTTP values, right? That would mean it gets populated 
with an empty string.... so I think the top one is actually occuring.)

This doesn't really make sense to me. Shouldn't a null or empty string 
value automatically convert to null in an Integer object?

Thanks,
Aaron





Brian Bonner wrote:

> 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
>
> Brian
>
> 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 <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
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>
>>>
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
>


Mime
View raw message