File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes how to use java.sql.Date type property in ActionForm?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to use java.sql.Date type property in ActionForm??" Watch "how to use java.sql.Date type property in ActionForm??" New topic
Author

how to use java.sql.Date type property in ActionForm??

divya chamarti
Ranch Hand

Joined: Jul 28, 2006
Posts: 56
can any body help me to tell how to support a property of type java.sql.Date for dealing with a form in struts for ValidatorForm as a super class of my FormBean class
as i'm declaring :
the property as
private java.sql.Date expiryDate;
//getter and setter for expiryDate property.
but my apllication doesn't work
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
You need to declare the Expiry date as String object in the ActionForm.
- The parameters received from client through HTTP are String types
- Struts cannot determine what the value is from String. You can typically pass long value for date or date in different format (dd-mm-yyyy or mm/dd/yyyy ...) so it's difficult for struts to the conversion.

Plus it's advantages to declare the properties as String so that you can populate the HTML Form element back with the values user entered if there is an error.

The best way is to declare a String property and have an extra method (apart from getter and setter) like getExpiryDateAsObject() method in the ActionForm which will use the SimpleDateFormatter to format the string to Date object or do this conversion up at either controller or modal objects.
divya chamarti
Ranch Hand

Joined: Jul 28, 2006
Posts: 56
thanks purushothaman
but i want that to be of of java.sql.Date type only and now my application is working properly but there is one problem still i'm facing i.e.
if i'm giving the date format input in the form as dd-mm-yyyy then it is working fine but in case if user doesn't provide any data or wrong format data i want it to be there must be approprite message but in my case i'm getting org.struts.beanUtils.conversionError
so how can i proceed for this???
thanks in advance
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by divya chamarti:
in case if user doesn't provide any data or wrong format data i want it to be there must be approprite message but in my case i'm getting org.struts.beanUtils.conversionError

This is precisely why Purushothaman and any experienced Struts developer will advise you to use a String. Using a Date object works great as long as the user enters valid data, but doesn't work so great if they don't.

Validation in Struts is geared towards Strings because that's what HTML sends to the server. If your ActionForm uses a String, you can validate the date String first, and then if it passes validation, you can convert it to a Date object. If it doesn't pass validation, you can return an error message. You can validate the date either by using the Struts Validation Framework, or by overriding the validate() method of your ActionForm.

If you use a Date object in your ActionForm, you get that ugly BeanUtils.conversionError when the user enters invalid data.


Merrill
Consultant, Sima Solutions
 
 
subject: how to use java.sql.Date type property in ActionForm??
 
Similar Threads
How to define custom data type in Schema
java.sql.Date - setting a future date?
Simple mapping issue for Hibernate newbie
what's the difference?
type casting issue