ConvertUtils [HELP]

Discussion in 'Java' started by Quinten Miller, Jun 23, 2004.

  1. 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.DateLocaleConverter.
    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(java.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/beanutils/apidocs/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.copyProperties(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
    Quinten Miller, Jun 23, 2004
    #1
    1. Advertising

  2. Quinten Miller

    jackie Guest

    you should register it in your customised Servlet class

    Quinten Miller <> wrote in message
    news:...
    > 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.DateLocaleConverter.
    > 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(java.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/beanutils/apidocs/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.copyProperties(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
    jackie, Jun 23, 2004
    #2
    1. Advertising

  3. 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(java.util.Date.class).
    > > I registered the custom converters in a custom Action Servlet
    > > (Struts).



    Cheers
    Quinten Miller, Jun 23, 2004
    #3
  4. Quinten Miller

    jackie Guest

    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 <> wrote in message
    news:...
    > 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(java.util.Date.class).
    > > > I registered the custom converters in a custom Action Servlet
    > > > (Struts).

    >
    >
    > Cheers
    jackie, Jun 24, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    747
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    679
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    824
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    669
    Eliyahu Goldin
    Mar 9, 2006
  5. Buster

    Help, Help, Help

    Buster, Oct 4, 2003, in forum: Java
    Replies:
    3
    Views:
    464
    Saager
    Oct 30, 2003
Loading...

Share This Page