struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ed Griebel" <edgrie...@gmail.com>
Subject Re: DynaValidatorForm and BigDecimal problem
Date Thu, 01 Mar 2007 16:13:13 GMT
It has nothing to do with Struts, the error message is saying that
commons BeanUtils cannot convert the string from the form submit into
a BigDecimal. Turns out that your issue is BigDecimal is not as
permissive as Double when it converts a non-numeric string to a
BigDecimal. If a string is not a number, Double converts it to 0.0,
but BigDecimal will throw an exception, even for an empty string.

Struts copies screen fields to formbean fields in
RequestUtils.populate() before it does anything else including
validation. So, even though there's not a value in the field (actually
*because* there's no numeric value!) populate() re-throws the
exception that you are seeing.

To get it to work you could create and register a custom converter
that will do some checking on the string before it passes it to the
BigDecimal(String) constructor.  You will need a class that extends
org.apache.commons.beanutils.converters.BigDecimalConverter, and
register your custom converter by calling ConvertUtils.register(new
<your class>(), BigDecimlal.class);

Or, you could instead use an alternate approach that many (me
included) recommend, of having all formbean fields be Strings. You
would have a String display field and a second field that is a
non-string, and have this populated by your action after validation
succeeds.

-ed

On 3/1/07, Gundersen, Richard <Richard.Gundersen@london-scottish.com> wrote:
> Hi
>
> Has anyone had problems with BigDecimal and DynaValidatorForms?
>
> I've got a form with two fields (advisorFee and grossIncome) defined as
> java.lang.Double. I want to change them to java.math.BigDecimal because
> they are money fields.
>
> If I change advisorFee to BigDecimal, the validation fails when I enter
> a value into grossIncome, even though this is still a Double.
>
> This is the error I get:
>
> javax.servlet.ServletException: BeanUtils.populate
>
> org.apache.struts.util.RequestUtils.populate(RequestUtils.java:453)
>
> [Caused by]...
>
> org.apache.commons.beanutils.ConversionException
>
> org.apache.commons.beanutils.converters.BigDecimalConverter.convert(BigD
> ecimalConverter.java:117)
>
> This doesn't make any sense to me. Is it possible that it's a bug in
> 1.3.5? Why should changing the definition of one field change the
> behaviour of another (they are not linked in any way e.g. in the
> validation rules)
>
>
> Regards
>
> Richard Gundersen
> Java Developer
>
> Email: richard.gundersen@london-scottish.com
> Phone: 01618302398
> Fax: 01618342536
> London Scottish Bank plc
> 24 Mount Street
> Manchester
> M2 3LS
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message