struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <>
Subject Re: Struts form handling (was: date validation)
Date Mon, 26 Apr 2004 17:27:36 GMT
Theoretically you're right about the 3 data types but for me the issues are
all in the String ---> java type conversion. We have a home cooked db engine
that handles DynaBeans and including type conversion for the database.

Personally I have no problem populating my (Dyna)ActionForm from the
database (except having to do the conversion from java type ---> String) and
I don't have to "hack alot of code together" to do it and I find embedding
DynaBeans simple rather than impossible.

I do it using the "LazyDynaBean" and "LazyValidatorForm" I wrote which are
posted here:

Having said that, if I was starting my app today I probably would opt for
Hubert's FormDef  stuff - I know he replied to your post but just for
completeness I'll repeat his links here:


----- Original Message ----- 
From: "Nathan Ewing" <>
To: "Struts Users Mailing List" <>
Sent: Monday, April 26, 2004 5:12 PM
Subject: RE: date validation

For me I see the major issue being that in reality all
web-based/database forms actually have 3 data types they have to deal

1. The Database - This is the data type that data is stored in the
database as (For example Date, DateTime, Integer, Double, VarChar, and
many variations thereof).
2. Action - This is where the form treats the data is the form that is
useful for users (for example Currency, or Date, or Phone Number).
3. Form - The form itself basically has one data type, String.

Struts is not really well equipped to deal with the problems resulting
from all this data conversion, especially if you want to use DynaBeans.

My opinion is that dynabeans are currently a bit of a hack, in that they
don't really fit well into the rest of the struts framework.  If you
want to prepopulate a dynabean from a database you have to hack a lot of
code together to make things run correctly.  It's also currently
impossible to embed one DynaBean into another.  Really dynabean
conversions should be part of the validation framework, and should
happen just before, or as part of that section.  Otherwise you run into
what is happening now, you either use all String types, or get
Exceptions thrown before you ever get access to the data.


-----Original Message-----
From: Niall Pemberton []
Sent: Monday, April 26, 2004 11:57 AM
To: Struts Users Mailing List
Subject: Re: date validation

The real issue (IMO) here is that "conversion" is the first step in
validation and you have to identify the date format to the validator
handles internationalization) - but struts/validator then does nothing
the results of that conversion - ending up either with Michaels scenario
where his form bean is defined as a date type and BeanUtils has to
what the validator is doing or with the "String" properties alternative,
then you end up having to re-convert the value in the Action again.

Its a shame that those "validator" conversions (not just for date types)
just thrown away and end up having to be duplicated - whatever the


----- Original Message ----- 
From: "Joe Germuska" <>
Sent: Monday, April 26, 2004 3:53 PM

> This is actually a fairly complicated question.  All of Struts
> automatic bean population relies implicitly on the commons-beanutils
> package (  Ultimately,
> this is really a question about that library, but obviously it's
> important to Struts users, so there's no reason to shuffle you off to
> commons-dev for an answer.
> This is mostly invisible, because BeanUtils comes with standard
> converters for most types.  However, since there isn't a single
> universal String format for dates, there can be no pre-registered
> date converter.
> So, in general, the solution is to register a converter that knows
> the string format.  Originally, "registration" was a static method on
> the ConvertUtils class
> mostly because everything on ConvertUtils (and in the BeanUtils
> package in general) were implemented as static methods.  It was later
> realized that this might not be suitably flexible, and bean-instance
> forms were created.  However, Struts has never been changed to use
> this alternate syntax.
> So by this point, you might agree that the simplest answer is tomake
> the form property type "String" instead of "Date", and do the
> conversion inside your Action before passing it in to your
> application (model layer).  Alternatively, you could write a FormBean
> class (instead of using DynaValidatorForm) which had a String setter
> for the property but an alternate Date getter.
> The biggest problem with doing this in a framework level way is that
> even within a single application, you probably can't commit to a
> single String format for dates -- what if sometimes you need people
> to enter times, and other times dates, and other times both?  Also,
> depending on your design principles, it may not be acceptable to have
> even a single date format for an internationalized application.
> If you really had a single input date format, then you might just
> write a Struts PlugIn that looked like this (warning, pseudocode)
> public class RegisterDateFormatPlugIn implements PlugIn {
>    public void setDateFormat(String string) {...}
>    public String getDateFormat() {...}
>    public void init(ActionServlet servlet, ModuleConfig config)
> {
>      ConvertUtils.register(new MyDateConverter(this.getDateFormat()),
> java.util.Date.class);
> }
>   private static class MyDateConverter implements Converter
> {
>    private MyDateConverter(String dateFormat)
>   {
>   }
>    // simple implementation of o.a.c.beanutils.Converter interface...
> }
> }
> That would solve it in a general way for all String <->
> java.util.Date conversions done by BeanUtils in Struts.
> Joe
> At 4:19 PM +0200 4/26/04, Michael Kastner wrote:
> >Hello,
> >
> >maybe someone can help me. I am trying to use DynaValidatorForm to
> >enter (amongst other fields) a date field.
> >
> >So, my form bean definition contains this property setting:
> >
> ><form-property name="paymentDue" type="java.util.Date" />
> >
> >In my validation defintion, I use
> >
> ><field
> >     property="paymentDue"
> >     depends="required,date">
> >     <msg
> >         name="required"
> >         key="invoiceeditform.error.paymentdue.required"/>
> >     <var>
> >         <var-name>datePattern</var-name>
> >         <var-value>dd.MM.yyyy</var-value>
> >     </var>
> ></field>
> >
> >However, when I send the form, this exception is being thrown:
> >
> >org.apache.commons.beanutils.ConversionException: Cannot assign
> >value of type 'java.lang.String' to property 'paymentDue' of type
> >java.util.Date'
> >...
> >...
> >
> >
> >Can anyone give me a hint, if there's anything wrong with my
> >settings? I've checked with similar questions in this list and some
> >documentations. If I am not totally off track the validator should
> >be able to read date strings and convert them to java.util.Date
> >objects.
> >
> >Any help is very much appreciated.
> -- 
> Joe Germuska
>        "Imagine if every Thursday your shoes exploded if you tied them
> the usual way.  This happens to us all the time with computers, and
> nobody thinks of complaining."
>              -- Jef Raskin
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

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

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

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

View raw message