Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ConvertUtils [HELP]

Reply
Thread Tools

ConvertUtils [HELP]

 
 
Quinten Miller
Guest
Posts: n/a
 
      06-23-2004
Hi All

I have been developing with the org.apache.commons.beanutils
collection and have come across a quirk!!!

I have written a custom converter that extends
org.apache.commons.beanutils.locale.converters.Dat eLocaleConverter.
I have done this because I need the converter to return a default
value if an exception is thrown while passing a java.util.Date. My
problem is
that even though I register the converter I have written it doesn't
seem to be picked up by objects other than the current object it is
registered
in when using org.apache.commons.beanutils.ConvertUtils.lookup(j ava.util.Date.class).
I registered the custom converters in a custom Action Servlet
(Struts).

Does anyone know how the org.apache.commons.beanutils.ConvertUtils
object works and how to make a custom converter available application
wide. (Including BeanUtils classes that I call)?

The code for creating and registering the custom converters is below.
The

DateLocaleConverter is of package org.mcri.tissueculture.util

static {
Locale austLocale = new Locale("en", "au");
SqlDateLocaleConverter sqlDateConverter = new
SqlDateLocaleConverter(null,
austLocale, "dd-MM-yyyy");
sqlDateConverter.setLenient(false);
SqlTimestampLocaleConverter timestampConverter = new
SqlTimestampLocaleConverter(null,
austLocale, "dd-MM-yyyy");
timestampConverter.setLenient(false);
// The DateLocaleConverter Below is of package
org.mcri.tissueculture.util
DateLocaleConverter dateConverter = new
DateLocaleConverter(null, austLocale, "dd-MM-yyyy");
dateConverter.setLenient(false);
dateConverter.setDefault(true);

StringLocaleConverter stringConverter = new
StringLocaleConverter(null,
austLocale, "dd-MM-yyyy");

// Register the converters with ConvertUtils.
ConvertUtils.register(sqlDateConverter, java.sql.Date.class);
ConvertUtils.register(timestampConverter,
java.sql.Timestamp.class);
ConvertUtils.register(dateConverter, java.util.Date.class);
ConvertUtils.register(stringConverter, String.class);
}

I am using:
J2SE 1.4.2_04
BeanUtils 1.6
Tomcat 4.1
Struts 1.1

Reasoning:

In the API for DateLocaleConverter
(http://jakarta.apache.org/commons/be...ocs/index.html) it
states that if you use a constructors that accepts a default
value(Object) then it will "Create a LocaleConverter that will return
the specified default value if a conversion error occurs.".

This is ideal, I would rather a default value than a exception when
using org.apache.commons.beanutils.BeanUtils.copyPropert ies(Object
dest, Object orig). Once the exception is thrown the rest of the
origin object is not copied into the destination object. I validate
all dates using a Validator Framework. The problem I have is that
sometimes dates are left blank on the forms and it tries to pass "" as
a date and subsequently throws and exception.

The reason for the exception being thrown lies within the
BaseLocaleConvert class that DateLocaleConverter extends. I checked
the source of BaseLocaleConvert and found that the boolean Default is
set to false with this comment... Should we return the default value
on conversion errors? There is no method supplied within the any of
the subclasses that allows this boolean to be set. I decided to extend
the DateLocaleConverter with one minor modification, a method to
modify the boolean default.

I then registered this object with the ConvertUtils in my Action
Servlet (Struts) so that it would be registered at the start of my
application. This worked fine when I checked the returned object from
ConvertUtils.lookup(java.util.Date.class). When I checked the value
returned in other objects within my application they returned the
standard converter and not the one I had registered, same goes for the
converter returned in the BeanUtils object.


Thank you to anyone who can help me
 
Reply With Quote
 
 
 
 
jackie
Guest
Posts: n/a
 
      06-23-2004
you should register it in your customised Servlet class

Quinten Miller <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi All
>
> I have been developing with the org.apache.commons.beanutils
> collection and have come across a quirk!!!
>
> I have written a custom converter that extends
> org.apache.commons.beanutils.locale.converters.Dat eLocaleConverter.
> I have done this because I need the converter to return a default
> value if an exception is thrown while passing a java.util.Date. My
> problem is
> that even though I register the converter I have written it doesn't
> seem to be picked up by objects other than the current object it is
> registered
> in when using

org.apache.commons.beanutils.ConvertUtils.lookup(j ava.util.Date.class).
> I registered the custom converters in a custom Action Servlet
> (Struts).
>
> Does anyone know how the org.apache.commons.beanutils.ConvertUtils
> object works and how to make a custom converter available application
> wide. (Including BeanUtils classes that I call)?
>
> The code for creating and registering the custom converters is below.
> The
>
> DateLocaleConverter is of package org.mcri.tissueculture.util
>
> static {
> Locale austLocale = new Locale("en", "au");
> SqlDateLocaleConverter sqlDateConverter = new
> SqlDateLocaleConverter(null,
> austLocale, "dd-MM-yyyy");
> sqlDateConverter.setLenient(false);
> SqlTimestampLocaleConverter timestampConverter = new
> SqlTimestampLocaleConverter(null,
> austLocale, "dd-MM-yyyy");
> timestampConverter.setLenient(false);
> // The DateLocaleConverter Below is of package
> org.mcri.tissueculture.util
> DateLocaleConverter dateConverter = new
> DateLocaleConverter(null, austLocale, "dd-MM-yyyy");
> dateConverter.setLenient(false);
> dateConverter.setDefault(true);
>
> StringLocaleConverter stringConverter = new
> StringLocaleConverter(null,
> austLocale, "dd-MM-yyyy");
>
> // Register the converters with ConvertUtils.
> ConvertUtils.register(sqlDateConverter, java.sql.Date.class);
> ConvertUtils.register(timestampConverter,
> java.sql.Timestamp.class);
> ConvertUtils.register(dateConverter, java.util.Date.class);
> ConvertUtils.register(stringConverter, String.class);
> }
>
> I am using:
> J2SE 1.4.2_04
> BeanUtils 1.6
> Tomcat 4.1
> Struts 1.1
>
> Reasoning:
>
> In the API for DateLocaleConverter
> (http://jakarta.apache.org/commons/be...ocs/index.html) it
> states that if you use a constructors that accepts a default
> value(Object) then it will "Create a LocaleConverter that will return
> the specified default value if a conversion error occurs.".
>
> This is ideal, I would rather a default value than a exception when
> using org.apache.commons.beanutils.BeanUtils.copyPropert ies(Object
> dest, Object orig). Once the exception is thrown the rest of the
> origin object is not copied into the destination object. I validate
> all dates using a Validator Framework. The problem I have is that
> sometimes dates are left blank on the forms and it tries to pass "" as
> a date and subsequently throws and exception.
>
> The reason for the exception being thrown lies within the
> BaseLocaleConvert class that DateLocaleConverter extends. I checked
> the source of BaseLocaleConvert and found that the boolean Default is
> set to false with this comment... Should we return the default value
> on conversion errors? There is no method supplied within the any of
> the subclasses that allows this boolean to be set. I decided to extend
> the DateLocaleConverter with one minor modification, a method to
> modify the boolean default.
>
> I then registered this object with the ConvertUtils in my Action
> Servlet (Struts) so that it would be registered at the start of my
> application. This worked fine when I checked the returned object from
> ConvertUtils.lookup(java.util.Date.class). When I checked the value
> returned in other objects within my application they returned the
> standard converter and not the one I had registered, same goes for the
> converter returned in the BeanUtils object.
>
>
> Thank you to anyone who can help me



 
Reply With Quote
 
 
 
 
Quinten Miller
Guest
Posts: n/a
 
      06-23-2004
I do register it in my custom Action Servlet.
Any other suggestions?

> you should register it in your customised Servlet class


> org.apache.commons.beanutils.ConvertUtils.lookup(j ava.util.Date.class).
> > I registered the custom converters in a custom Action Servlet
> > (Struts).



Cheers
 
Reply With Quote
 
jackie
Guest
Posts: n/a
 
      06-24-2004
Did you config your web.xml correctly ?
Are you sure you are talking about the main Action Servlet of Struts ?

<servlet-name>action</servlet-name>
<servlet-class>com.xxx.xxxxxActionServlet</servlet-class>

Also, you need to write your own Converter method or override the default
behavour.


Quinten Miller <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I do register it in my custom Action Servlet.
> Any other suggestions?
>
> > you should register it in your customised Servlet class

>
> > org.apache.commons.beanutils.ConvertUtils.lookup(j ava.util.Date.class).
> > > I registered the custom converters in a custom Action Servlet
> > > (Struts).

>
>
> Cheers



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments