ConvertUtils [HELP]

Q

Quinten Miller

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
 
J

jackie

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top