I wrote my own method based on having three select dropdown boxes for the date - day, month and year.

Rather than do it as a type converter, I did it as an interceptor, with three getters/setters in the action (getStartTime_Day, getStartTime_Month, getStartTime_Year). The interceptor uses reflection when it encounters a Date setter (with a suitable annotation) in the action to look up these fields and parse them into a Date for setting. Lets me do validation at the same time.

Andy.

On 1 May 2009, at 12:44, Richard Sayre wrote:

I think I am going to write my own.  After experimenting with the date
time picker it only seems to convert the date if its in this format
mm/dd/yy. In my application the user can choose their own date format.
When I have a format like 1 May 2009, the datetime picker value does
not get converted.  It is expecting to set a String instead of a Date.
I am still trying to get my own converter to work right.  If I get
something written I will share it with the group.



On Thu, Apr 30, 2009 at 10:19 PM, Zoran Avtarovski
<zo...@sparecreative.com> wrote:
That¹s exactly right but sometimes you want to use a format that isn¹t the short format for the locale and I suspect that¹s what is happening with
Richard.

He has two choices, either configure his date time picker to produce the date in the required format, or as suggested write your own converter. My only concern with Matt¹s version is that the conversion exception is buried
which will have implications for validation.

Z.



Hi Richard,

Have a look at http://struts.apache.org/2.1.6/docs/type-conversion.html
It says that "dates - uses the SHORT format for the Locale associated with the
current request"

May be this will help.

Thank you.
Regards,
Kishan.G

Senior Software Engineer.
www.spansystems.com




-----Original Message-----
From: Richard Sayre [mailto:richardsa...@gmail.com]
Sent: Thursday, April 30, 2009 4:14 PM
To: Struts Users Mailing List
Subject: Re: Submitting a Date to an Action

Thank you.  I will give it a try.

On Thu, Apr 30, 2009 at 1:22 AM, Matt Jiang <matt.ji...@gmail.com> wrote:
Hi

It is nothing about date picker tag. You can have a Converter to convert
String to Date and vice versa.
Below is my implementation for your reference:
(For DateUtil, please replace with yours)

public class DateConverter extends StrutsTypeConverter {

private static final String PATTERN = DateUtil.PATTERN_YYYY_MM_DD;

 @Override
 /**
  * Converts one or more String values to the specified class.
  *
  * @param context the action context
* @param values the String values to be converted, such as those
submitted from an HTML form
  * @param toClass the class to convert to
  * @return the converted object
  */
public Object convertFromString(Map context, String[] values, Class
toClass) {

   Date returnObject = null;
   String value = values[0];
   if (value != null && !value.trim().equals("")) {
     try {
       returnObject = DateUtil.parseDate(value, PATTERN);
     } catch (ParseException e) {
       // Just to ignore the parse exception
     }
   }
   return returnObject;
 }

 @Override
 /**
  * Converts the specified object to a String.
  *
  * @param context the action context
  * @param o       the object to be converted
  * @return the converted String
  */
 public String convertToString(Map context, Object o) {

   Date date = (Date) o;
   String formatedDate = DateUtil.dateFormater(date, PATTERN);
   return formatedDate;
 }
}



On Thu, Apr 30, 2009 at 1:19 AM, Richard Sayre
<richardsa...@gmail.com>wrote:

I have an Action with a date attribute

private Date myDate;

public void setMyDate(Date myDate) {
this.myDate = myDate;
}

I have a form that has a date picker (not the struts 2 date picker) that populates a text field. When I submitt the form to my action the
property does not get set because Struts 2 is looking for
setMyDate(String myDate).

How do I tell Struts that the field is a Date?


Thank you,

Rich

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to