Actually, BeanUtils can convert from String to a variety of Java types;
Date just doesn't happen to be one of them by default. However, it's
generally recommended to use String properties in action forms exclusively
so that invalid user inputs can be re-displayed as entered.
For example, if you ask for a data and the user enters 'Barney', date
parsing is going to fail wherever it occurs. If the form uses type Date,
there's no way you can represent the invalid input 'Barney' in the form, so
you can't redisplay it to the user for correction. Hence use of String
valued form properties.
If you don't care about round-tripping bad inputs this way, you can always
register additional converters with BeanUtils to handle the String to X
conversions you need.
L.
Borislav Sabev wrote:
Hi all,
The problem I'd like to discuss is how Validator works with form
properties. IMHO there is a contradiction (or maybe I don't know how to
resolve this problem - in this case, please help me) with types of Form
properties. The end result is that if I want to use the Validator, all
my form properties MUST be java.lang.String, otherwise Struts (in fact
BeanUtils and ConvertorUtils) throws
org.apache.commons.beanutils.ConversionException. So why should specify
the type of form properties if Validator so and so doesn't work with any
other type than java.util.String.
To clarify the problem here is an example:
I have this form:
<form-bean name="testValidation"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="startDate" type="java.util.Date" />
<form-property name="endDate" type="java.util.Date" />
</form-bean>
and here is the snippet of my validation.xml file that should check that
the dates entered are in some special format:
<form name="testValidation">
<field property="startDate"
depends="date">
<arg key="startDate"/>
<var>
<var-name>datePattern</var-name>
<var-value>yyyy-MM-dd</var-value>
</var> </field>
<field property="endDate"
depends="date">
<arg key="endDate"/>
<var>
<var-name>datePattern</var-name>
<var-value>yyyy-MM-dd</var-value>
</var> </field>
</form>
The idea behind this is that I want the parameters that are coming with
the request, to be "translated" to their expected types once and only once!
So if you try this, you will get
org.apache.commons.beanutils.ConversionException (thrown from
RequestProcessor if I remember correctly).
As soon as I set this
<form-property name="startDate" type="java.lang.String" />
<form-property name="endDate" type="java.util.String" />
the validator starts to work correctly.
So here I see at least 2 problems:
1. I'm forced again to parse the String properties of the form to proper
java.util.Date objects (or whatever other type it has to be)
2. because of 1. , my date format string now is in 2 places instead
of one place i.e. I have a support problem
So IMHO this is a "framework" inconsistency since.
Please tell me if I do something wrong or this is one of "known
limitations".
Borislav
------------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
Laurie, Open Source advocate, Java geek and novice blogger:
http://www.holoweb.net/~laurie/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]