struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Krygowski <>
Subject java.util.Dates and Struts
Date Mon, 23 Dec 2002 16:49:04 GMT
I've been chatting with Rick Reumann, author of the very nice
DateBeanUtilsConverter solution to dealing with java.util.Dates within
Struts (and by extension BeanUtils).  I'm broadening the discussion to the
struts group because I'd like to bounce my observations off of a wider
audience.  The conversation about Rick's code was very extensive on the
struts-user mailing list, so I hope everyone isn't tired of talking about
this one.

In a nut shell, after absorbing the coolness of this approach, I found
myself questioning how this solution would work for values going from Struts
to a JSP.  That is, I have a Date object in my ActionForm, and I want it
rendered to my JSP using <html:text> and I want it rendered using the
formatting I specify.  Rick's solution included a Date->String partner
(StringBeanUtilsConverterDate) for DateBeanUtilsConverter (String->Date).
When I first set up an Action with these two converters, I noticed that I
could submit a form with a date string formatted according to my predefined
format and it was properly transformed into a java.util.Date.  However,
java.util.Date always seemed to be rendered as if it had .toString() called
on it.  This lead me to believe that the StringBeanUtilsConverterDate was
never called.  I've traced the code all the way through and now I'm (almost)
positive that it isn't getting called.

This is my test:
	String input = null;

      // check to make sure the datatype is right
      input = "2002-03-17";
      Date dateValue = (Date)ConvertUtils.convert(input, Date.class);
      assertTrue(dateValue instanceof Date);

      // try to get a value from a Map
	HashMap values = new HashMap();
      values.put("today", "2002-03-17");
      MockForm mockForm = new MockForm();
      BeanUtils.copyProperties(mockForm, values);

	// try to put a value in a map from the form, will it be formatted?
      Map beanProps = BeanUtils.describe(mockForm);
The last block of code simulates the activity during the rendering of a
struts page.  Note that although I don't show it in the snippet, I do have a
static initializer on the test that loads both converters as prescribed by
Rick in his mailing list posting.  In BeanUtils.describe, I found calls to
getNestedProperty() which contains the following code:

    public static String getNestedProperty(Object bean, String name)
            throws IllegalAccessException, InvocationTargetException,
            NoSuchMethodException {

        Object value = PropertyUtils.getNestedProperty(bean, name);
        return (ConvertUtils.convert(value));


So, as you can see, Struts (or BeanUtils) always defaults to using the
simple String conversion, which as I mentioned before, does not refer to the
Converter collection.

If this is the case, how can I render my <html:text> input boxes using the
right date format?  As I mentioned to Rick when I first asked him for help,
I may have easily missed something here out of ignorance.  I have tried to
trace things through as much as possible.  Thanks again to Rick for his help
so far!



To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message