On 23-07-2008 at 04:53, Nathan McDonald wrote:
> 
> Working on a web form and we're required to have seperate fields for date and
> time input.  With a java.util.Date property on the bean, both fields can
> reference this and show the default settings easily:
[...]

> However they are obviously both using the same field name, so when attempting 
> to
> submit the form this will not work (in actual fact I get the first value of 
> the
> plain Date being set).
> 
> To get this to work as intended I've had to do the steps shown below.
[cut: example where two properties are used for input, and one for output]

> The point of my post is: Is this the correct way of doing something this? It
> seems like a lot was required, which is setting of warning bells that I've
> missed something obvious and simpler.

Your solution works fine for what you want, and is sufficiently clean for
pragmatic purists. Especially, you need to have two fields for input, as then
the error messages will be correct.

Another, similar solution is to provide one real and two transient
properties: datetime (the real one), date and time. Something like this (I
left out the methods getTime() and setTime(Date) for brevity):

        private Calendar datetime;
        public Date getDatetime() { return datetime.getTime(); }
        public void setDatetime(Date datetime) { 
this.datetime.setTime(datetime); }

        /**
         * Return the date portion of [EMAIL PROTECTED] #datetime}.
         *
         * @return a Date with the time fields set to midnight
         */
        public Date getDate() {
                Calendar calendar = datetime.clone();
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                return calendar.getTime();
        }

        /**
         * Set the date portion of [EMAIL PROTECTED] #datetime}.
         *
         * @param date the new date
         */
        public void setDate(Date date) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                datetime.set(Calendar.HOUR_OF_DAY, 
calendar.get(Calendar.HOUR_OF_DAY));
                datetime.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
                datetime.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
                datetime.set(Calendar.MILLISECOND, 
calendar.get(Calendar.MILLISECOND));
        }


Oscar

-- 
   ,-_   The essence of all slavery consists in taking the produce of
  /() )  another's labor by force. It is immaterial whether this force
 (__ (   be founded upon ownership of the slave or ownership of the
=/  ()   money that he must get to live.  -- Leo Tolstoy (1828-1910)

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to