Firstly, if you have java.util.Dates in your ActionForm, then BeanUtils
*will* throw an exception, so you need to at least change the dates in your
ActionForm (to Strings). I think the other values will work because there
are converters for Long etc., but I'm not sure this is best practice.

As I understand it, you should only ever use String and boolean properties
in an ActionForm because we're talking about an HTML form buffer here, not a
business object. I've been looking for the definitve reference on this -
I've found it before, but I can't find it right now.

Anyway, you've got only two options. Define your date fields as String or
java.sql.Date in your action form. If you do the latter then your users will
have to enter dates in the SQL date format which is yyyy-mm-dd, and
BeanUtils will cope. If you want them to be able to enter more user friendly
formats you must use Strings or find/create your own BeanUtils converter. I
haven't looked at that.

Ok, so now your field definition for your ActionForm looks like this:

  <form-property name="startDate" type="java.lang.String" />

Form validation will work correctly (using validator) because it uses String
parsing to validate date formats and if there is an error then the exact
String the user enters will be redisplayed for them to correct. (Another
reason to use Strings & booleans only).

This is where it gets messy (at least for me). In the Action I use BeanUtils
to bulk copy most of my properties from the ActionForm, but copy over the
Dates by parsing the Strings and setting my transfer bean manually. I don't
claim this to be elegant, but it works.

 TestBean myBean = new TestBean();

  // Get a Map of your form
  Map map = BeanUtils.describe(myBean);

  // Remove the problematic properties
  map.remove("startDate");

  // Auto-populate (with conversions) all the other properties
  BeanUtils.populate(myBean, map);

  // Manually set the other properties
  // Note: I'm using a fixed format here for simplicity,
  // You should probably condsider parsing based on your
  // Locale or your user's Locale
  SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); /
  try {
    java.util.Date date =
       df.parse(BeanUtils.getProperty(form, "startDate"));
       myBean.setStartDate(date);
  } catch (ParseException e) {
       errors.add(
           ActionErrors.GLOBAL_ERROR,
               new ActionError("errors.date", "Date"));
  }

An alternative might be to split the date into three fields then you could
either combine these in your Action or use a helper on your ActionForm to
return a SQL formatted Date. A helper method could also be used to parse a
single field, returning a value that BeanUtils can cope with.

If anyone knows a better way of handling Dates from forms then please share
because this feels clunky and I don't like clunk.

Steve

> -----Original Message-----
> From: Mick Knutson [mailto:[EMAIL PROTECTED]
> Sent: May 29, 2003 12:24 PM
> To: [EMAIL PROTECTED]
> Subject: RE: URGENT: javax.servlet.ServletException: BeanUtils.populate
>
>
> Do you mean change m dynaForm to java.sql.Date types, then use a
> DateUtil in
> my Action Class to convert them? (to what then?)
>
>
> >From: "Steve Raeburn" <[EMAIL PROTECTED]>
> >Reply-To: <[EMAIL PROTECTED]>
> >To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
> >Subject: RE: URGENT: javax.servlet.ServletException: BeanUtils.populate
> >Date: Thu, 29 May 2003 12:20:56 -0700
> >
> >I think it's your dates that might be the problem.
> >
> >BeanUtils does not have a supplied converter for java.util.Date.
> Try using
> >java.sql.Date
> >
> >You may need a helper method on your form that returns a correctly
> >formatted
> >SQL date (yyyy-mm-dd).
> >
> >Steve
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> _________________________________________________________________
> MSN 8 with e-mail virus protection service: 2 months FREE*
> http://join.msn.com/?page=features/virus
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to