struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Zoran Avtarovski <zo...@sparecreative.com>
Subject Re: Submitting a Date to an Action
Date Fri, 01 May 2009 00:49:23 GMT
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:richardsayre@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.jiang@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
>> <richardsayre@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-unsubscribe@struts.apache.org
>>> >> For additional commands, e-mail: user-help@struts.apache.org
>>> >>
>>> >>
>> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message