The problem is the use of a Date type for the form property. The first thing Struts does is populate the form bean from the request; this has to happen before validation, so the validation has a form to work with.

This is one of the many reasons it's recommended to make your form bean properties be type String and convert to native types after validation. So, you would declare the form property to be of type String in struts-config, then in your action's execute() method do the conversion to Date, after the validator has already confirmed that the string is in fact a valid date.

If you absolutely must have Date-type form bean properties, another option is to make BeanUtils convert empty values to null instead of raising an exception. You do that by creating and registering a new converter in BeanUtils. See the BeanUtils docs for more info if you want to try that approach.

L.

Yujun Liang wrote:
The exception happens before validation.

I aksed the same question before and I didn't get a clean solution yet, so
far, if you define it as a java.sql.Timestamp, you have to provide an input
in yyyy-mm-dd hh:mm:ss.fffffffff format

Did you try beanDef?

Or maybe you can change the default RequestProcessor to your own
RequestProcessor and when populating the actionForm form-bean, use
LocaleBeanUtil instead of BeanUtil, but I haven't done that yet.

Please let me know when you got something done since I am also struggling
with this. Thanks

Regards

On 11/17/05, Jesus Salvo Jr. <[EMAIL PROTECTED]> wrote:


Thanks ... I sort of worked around it by using java.sql.Date instead,
since the built-in converts in commons-beanutils does not really have a
java.util.Date converter,
only a java.sql.Date converter.

However, I still have one problem with this.

If a user leave the date field empty in the form, you will get the
exception below.

The problem with this it seems that the conversion happens BEFORE the
validation.
What's the point of having the following in validation.xml, if this
exception happens earlier ??

<field
property="preLiveDate"
depends="required,date">
<arg key="campaignForm.preLiveDate"/>

<var><var-name>datePattern</var-name><var-value>yyyy-MM-dd</var-value></var>
</field>


org.apache.commons.beanutils.ConversionException
at org.apache.commons.beanutils.converters.SqlDateConverter.convert(
SqlDateConverter.java:117)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(
ConvertUtilsBean.java:428)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(
BeanUtilsBean.java:1004)
at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java
:811)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
at org.apache.struts.action.RequestProcessor.processPopulate(
RequestProcessor.java:805)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java
:203)



On Thursday 17 November 2005 16:13, Yujun Liang wrote:

Here is some replies when I asked the same question before, hope it

helps.

Regards
----- Original Message -----
From: "JEROME RAULINE" <[EMAIL PROTECTED]>
To: "Yujun Liang" <[EMAIL PROTECTED]>
Sent: Friday, October 28, 2005 6:53 PM
Subject: java.util.Date in Action Form Bean


Hi,

You'd better use a String object, and then convert it into a
java.*.Date. This way, you can check the date format and tell the user
that it's wrong. Using a Date object, you'll have null if the format

is

not corresponding. And then, you don't know if the user used a wrong
date or if he does not fill it out at all.

Regards


I am working on a project using Struts. Struts uses BeanUtil to

populate

Action Form Bean. This is such a nice framework to work with, except,


BeanUtil doesn't support java.util.Date conversion. But when I use

java.sql.Timestamp, it asks for the format of


yyyy-mm-dd hh:mm:ss.fffffffff

Here is the exception when I enter other format,

Caused by: java.lang.IllegalArgumentException: Timestamp format must

be

yyyy-mm-dd hh:mm:ss.fffffffff


In a web application, it is not reasonable to ask user to input
yyyy-mm-dd

hh:mm:ss.fffffffff.


Also, it doesn't support Locale.
Do you have experience getting java.util.Date populated in a Java Bean

inside a FormBean?




----- Original Message -----
From: "Laurie Harper" <[EMAIL PROTECTED]>
To: <dev@struts.apache.org>
Sent: Saturday, October 29, 2005 2:17 AM
Subject: Re: java.util.Date in Action Form Bean


So far I'm doing this by registering a custom implementation of
Converter which I instantiate with a suitable instance of
SimpleDateFormat before calling BeanUtilsBean.copyProperties().

There's also the beanutils.locale package, which provides Locale-aware
conversions and includes support for java.util.Date (I think) out of

the

box. Unfortunately, it's not obvious how to use it. I posted a thread

on

the commons-user list asking for guidance, though I haven't seen a
response yet.

For what it's worth, here's a rough outline of doing this the 'easy'
(i.e. inelegant!) way:

final SimpleDateFormat sdf = ...
BeanUtilsBean utils = new BeanUtilsBean();
ConvertUtilsBean ctuils = utils.getConvertUtils();
cutils.register(new Converter() {
public Object convert(Class clazz, Object value) {
return sdf.parse((String) value);
},
Date.class);
utils.copyProperties(...

That's untested code, and the implementation of convert() should

incude

checks for value being null, not a String, etc and handling for if the
date can't be parsed... but it gives you the rough idea.

L.

----- Original Message -----
From: "Murray Collingwood" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Friday, October 28, 2005 3:44 PM
Subject: Re: java.util.Date in Action Form Bean


Hi

I add additional getters and setters for handling dates and prices

(and

other special


numeric formats). For example, in addition to the normal getter I code

an

additional


one to retrieve the date as a string.

public Date getDateStart() {
return dateStart;
}
public String getDateStartAsString() {
if (dateStart == null) return "";
return Focus.date2String(dateStart);
}


I then code up some static methods to format dates to strings and

similar

methods to


make the opposite translation (string back to date). For example, a
method

from my


Focus class related to the above code:

public static String date2String(Date date) {
return sdf.format(date);
}

And if you're desparate, here is the sdf definition:

private static SimpleDateFormat sdf = new

SimpleDateFormat("dd/MM/yyyy");

You can do the same thing with a Timestamp, depends how many fields

you

want to


declare on your form and how much data entry you want your user to do.

I

used a


similar method in my Focus class to get/set the date portion of the

timestamp value:

public static String timestamp2String(Timestamp ts) {
if (ts == null) return "";
return date2String(new Date(ts.getTime()));
}

HTH
mc

----- Original Message -----
From: "Richard Yee" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Saturday, October 29, 2005 12:48 AM
Subject: Re: java.util.Date in Action Form Bean


You should be aware that when using a java.text.DateFormat class or

its

subclass as a static variable in a bean that can be accessed via
multiple threads simultaneously, you need to synchronize access to it
because DateFormat is not thread-safe. So you should change the code
below to:

public static synchronized String date2String(Date date) {
return sdf.format(date);
}

or

public static String date2String(Date date) {
synchronized (date) {
return sdf.format(date);
}
}



This is from the DateFormat Javadoc:Synchronization

Date formats are not synchronized. It is recommended to create

separate

format instances for each thread. If multiple threads access a format
concurrently, it must be synchronized externally.

Regards,

Richard

----- Original Message -----
From: "Ryan Stewart" <[EMAIL PROTECTED]>
To: <dev@struts.apache.org>
Sent: Saturday, October 29, 2005 2:10 PM
Subject: Re: java.util.Date in Action Form Bean


Not to mention that DateFormats are not thread-safe, and I'm fairly

sure

Converter access is not synchronized.


----

On 11/17/05, Jesus Salvo Jr. <[EMAIL PROTECTED]> wrote:

Struts 1.2.7

How do u use a ActionForm formbean with a java.util.Date as a property

?

The form bean is defined as follows in struts-config.xml:

<form-bean name="campaignForm" type="
com.mig.provisioning.CampaignForm"/>

.. and the actual bean has the following methods:

public void setLiveDate(Date liveDate) {
this.liveDate = liveDate;
}
public Date getPreLiveDate() {
return preLiveDate;
}


I am getting a the exception below:

java.lang.IllegalArgumentException: Cannot invoke
com.mig.provisioning.CampaignForm.setPreLiveDate - argument type

mismatch

at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(
PropertyUtilsBean.java:1778)
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(
PropertyUtilsBean.java:1759)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty (
PropertyUtilsBean.java:1648)
at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(
PropertyUtilsBean.java:1677)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(
BeanUtilsBean.java:1022)
at org.apache.commons.beanutils.BeanUtilsBean.populate(

BeanUtilsBean.java

:811)

at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
at org.apache.struts.util.RequestUtils.populate (RequestUtils.java

:493)

at org.apache.struts.action.RequestProcessor.processPopulate(
RequestProcessor.java:805)
at
org.apache.struts.action.RequestProcessor.process(

RequestProcessor.java

:203)

at
org.apache.struts.action.ActionServlet.process(ActionServlet.java

:1194)

at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java

:432)

at javax.servlet.http.HttpServlet.service(HttpServlet.java :716) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:809)

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

--
Yujun Liang
[EMAIL PROTECTED]

--
Jesus M. Salvo Jr.
Senior Technical Consultant

COMMUNICATOR INTERACTIVE

www.communicator.com.au <http://www.communicator.com.au>
P +6129256 0914 F +612 9256 0922 M +61 409126699
Level 8, 9-13 Young Street, Circular Quay, Sydney. 2000
Copyright Communicator InteractivePty Limited 2005
CONFIDENTIAL INFORMATION owned exclusively by Communicator Interactive Pty
Limited. The information in this email and any attachments is privileged and
confidential, intended only for the use of the recipient or the intended
recipient. If you are not the intended recipient, any use of the information
is strictly prohibited. If you have received this communication in error,
please inform us immediately and delete the message and any attachments.

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





--
Yujun Liang
[EMAIL PROTECTED]



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

Reply via email to