You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Schneider, Eric" <es...@tiaa-cref.org> on 2002/07/16 17:33:45 UTC

Exception with null Date form field

Hi,

I've notice a lot of posts lately about the BeanUtils class.  I was just
wondering if someone could confirm if this is a previously reported bug that
has been fixed in the nightly builds.

I have a form bean that contains an attribute of type java.sql.Date.  While
attempting to save the values for this form and exception is thrown in
BeanUtils.populate()/SqlDateConverter.convert() methods if the user leaves
this date field blank.  The ActionForm's validate method is never reached,
so it appears that I can't catch this scenario.  Is there a resolution to
this?.....besides changing the form type to a string.

Thanks,
Eric

tomcat 4.0.3
struts 1.1b

javax.servlet.ServletException: BeanUtils.populate
	at
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:954)
	at
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:795)
	at
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:244)
	at
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1109)
	at
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:470)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

.........

root cause 

org.apache.commons.beanutils.ConversionException
	at
org.apache.commons.beanutils.converters.SqlDateConverter.convert(SqlDateConv
erter.java:150)
	at
org.apache.commons.beanutils.ConvertUtils.convert(ConvertUtils.java:359)
	at
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:604)
	at
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:952)
	at
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:795)
	at
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:244)
	at
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1109)
	at
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:470)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)


**********************************************************************
This message, including any attachments, contains confidential information intended for a specific individual and purpose, and is protected by law.  If you are not the intended recipient, please contact sender immediately by reply e-mail and destroy all copies.  You are hereby notified that any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited.
TIAA-CREF
**********************************************************************

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Exception with null Date form field

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 16 Jul 2002, Schneider, Eric wrote:

> Date: Tue, 16 Jul 2002 09:33:45 -0600
> From: "Schneider, Eric" <es...@tiaa-cref.org>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: 'Struts Users Mailing List' <st...@jakarta.apache.org>
> Subject: Exception with null Date form field
>
> Hi,
>
> I've notice a lot of posts lately about the BeanUtils class.  I was just
> wondering if someone could confirm if this is a previously reported bug that
> has been fixed in the nightly builds.
>
> I have a form bean that contains an attribute of type java.sql.Date.  While
> attempting to save the values for this form and exception is thrown in
> BeanUtils.populate()/SqlDateConverter.convert() methods if the user leaves
> this date field blank.  The ActionForm's validate method is never reached,
> so it appears that I can't catch this scenario.  Is there a resolution to
> this?.....besides changing the form type to a string.
>

In your form bean, this field should be a String, not a Date.  That is for
two reasons:

* Populating the form bean happens before validation, so you have
  to ensure that conversion will be successful.  It won't be if the
  user types an invalid value.  That's what happened to you here.

* If the user does type an invalid value, you want to be able to
  reproduce exactly what he or she *did* type, just like any GUI
  program will.

Conversion to a Date should happen in your Action, once you know that
validation has been successful and that you won't run into conversion
errors.  A copyProperties() method was added to BeanUtils that knows how
to do the conversions that is perfect for use in such circumstances.

But your form bean field should absolutely NOT be a Date.

> Thanks,
> Eric
>

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>