struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Sayre <richardsa...@gmail.com>
Subject Re: Submitting a Date to an Action
Date Fri, 01 May 2009 17:36:23 GMT
My date field is one text box with a Date Picker Widget.  The
converter does the valid date validation for me by attempting to parse
the date in the Util class.  If it returns null then I know I got a
parse exception.

I am including my convertFromString code below.  I need it to handle
both single dates and an array of dates.  My form always has a date
range, plus N number of date ranges to select.  I wrote it to work for
both.  Any criticisms or questions about the code are welcomed.

public Object convertFromString(Map context, String[] values, Class
toClass) throws TypeConversionException {

       ArrayList<Date> dates = new ArrayList<Date>();
        Date returnDates[] = null;

        if (toClass == Date.class ||
            toClass == Date[].class) {

            StrutsRequestWrapper srw = (StrutsRequestWrapper)
context.get("com.opensymphony.xwork2.dispatcher.HttpServletRequest");

            User u = (User)
srw.getSession().getAttribute(Constants.USER_SESSION_KEY);

            Util util = new Util();

            for (int i = 0; i < values.length; i++) {

                if (values[i].trim().length() > 0) { //all date ranges
are submitted even if they are empty


                    Date tmpDate = util.parseDate(values[i], u.getDateFormat());

                    dates.add(tmpDate);

                    if (tmpDate == null) { //parsing failed

                        throw new TypeConversionException("Could not
parse date [" + values[i] + "] using format [" + u.getDateFormat() +
"]");

                    }
                }
            }

             returnDates = dates.toArray(new Date[0]);

        }

        if (returnDates.length > 1) {
            return returnDates;

        }

        return returnDates[0];

    }


On Fri, May 1, 2009 at 12:32 PM, Andy Sykes <a.sykes@ucl.ac.uk> wrote:
> 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
>> <zoran@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: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
>>>>
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> 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
View raw message