This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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.
Joined: Jul 28, 2006
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
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.