You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Wick, Dan" <Da...@Donaldson.com> on 2010/03/30 16:34:51 UTC

textfield with Date object

Does anyone know how to change the format of dates coming in on a text input being backed by a java.util.Date in the Action?  If I enter dates in US format (ex: 3/31/2010 for March 31, 2010) everything is fine.  This requires a different style input though, using Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse it for a date myself, but I'm wondering if there's a Right way to do it.

--Dan

<-- Snip of jsp page -->
Date Of Birth*
<s:textfield name="dob" required="true" tabindex="4" size="30" cssClass="staticBody" cssErrorClass="formError"/>
<-- End snip of jsp page -->


<-- Snip of Action -->

	private Date dob;
	public Date getDob() {
		return dob;
	}
	@RequiredStringValidator(message="Date of Birth is required.", trim=true)
	public void setDob(Date dob) {
		this.dob = dob;
	}
<-- End snip of Action -->



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


RE: textfield with Date object

Posted by "Wick, Dan" <Da...@Donaldson.com>.
That's neato.  I can specify the locale in the link to this special page/action & thereby get my date to parse!

Thanks!

Dan

> -----Original Message-----
> From: Rene Gielen [mailto:gielen@it-neering.net]
> Sent: Wednesday, March 31, 2010 6:21 AM
> To: Struts Users Mailing List
> Subject: Re: textfield with Date object
> 
> Hi,
> 
> (copied from an earlier thread on numbers)
> 
> see
> http://struts.apache.org/2.1.8.1/docs/formatting-dates-and-numbers.html
> 
> It is important to know that Struts 2 has always a locale context
> request for each request, which affects both formating and parsing
> dates
> and numbers. The request locale is determined as follows:
> 
> 1. Request parameter
> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
> 2. Session attribute
> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
> 3. Browser preference
> 4. Server side JVM default
> 
> The first matching lookup wins.
> 
> The type conversion mechanism tries to parse dates and numbers in the
> current locale context, thus you should be fine when you ensure that
> you
> have a consistent locale request setting both for the first request
> (rendering the form and the current value in the textfield) and the
> second request (processing the submitted form).
> 
> - René
> 
> Wick, Dan schrieb:
> > I accept your answer, but Struts2 should just handle this! :-(
> >
> > I guess I'll write a number of helper methods like you did.
> >
> >> -----Original Message-----
> >> From: Greg Lindholm [mailto:greg.lindholm@gmail.com]
> >> Sent: Tuesday, March 30, 2010 10:14 AM
> >> To: Struts Users Mailing List
> >> Subject: Re: textfield with Date object
> >>
> >> (Seems like I just answered this question... but here it is again
> this
> >> time for Dates)
> >>
> >> In my experience it's better to define number (and time) input
> fields
> >> as String and perform the conversion and validation in your action
> >> using java.text.NumberFormat (and java.text.DateFormat).  This puts
> >> you in control of the
> >> process and let's you handle I18N issues that way you want to
> without
> >> fighting Struts.
> >>
> >> I typically have a couple of helper methods I put into the base
> Action
> >> class that simplify getting the users Locale and DateFormat.
> >> Depending on the app the users Locale may come from a Login object
> or
> >> the browser request (which Struts can retrieve) or from a cookie.
> >>
> >> On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan
> <Da...@donaldson.com>
> >> wrote:
> >>> Does anyone know how to change the format of dates coming in on a
> >> text input being backed by a java.util.Date in the Action?  If I
> enter
> >> dates in US format (ex: 3/31/2010 for March 31, 2010) everything is
> >> fine.  This requires a different style input though, using
> >> Day/Month/Year (31/3/2010).  I suppose I could take in a string &
> parse
> >> it for a date myself, but I'm wondering if there's a Right way to do
> >> it.
> >>> --Dan
> >>>
> >>> <-- Snip of jsp page -->
> >>> Date Of Birth*
> >>> <s:textfield name="dob" required="true" tabindex="4" size="30"
> >> cssClass="staticBody" cssErrorClass="formError"/>
> >>> <-- End snip of jsp page -->
> >>>
> >>>
> >>> <-- Snip of Action -->
> >>>
> >>>        private Date dob;
> >>>        public Date getDob() {
> >>>                return dob;
> >>>        }
> >>>        @RequiredStringValidator(message="Date of Birth is
> required.",
> >> trim=true)
> >>>        public void setDob(Date dob) {
> >>>                this.dob = dob;
> >>>        }
> >>> <-- End snip of Action -->
> >>>
> >>>
> >>>
> >>> -------------------------------------------------------------------
> --
> >>> 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
> >
> 
> --
> René Gielen
> IT-Neering.net
> Saarstrasse 100, 52062 Aachen, Germany
> Tel: +49-(0)241-4010770
> Fax: +49-(0)241-4010771
> Cel: +49-(0)163-2844164
> http://twitter.com/rgielen
> 
> ---------------------------------------------------------------------
> 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


Re: textfield with Date object

Posted by Rene Gielen <gi...@it-neering.net>.
The same problem as with any localized web app. Typical Struts 2
solution to explicitly switch the locale context:

<a href='<s:url><s:param name="request_locale"
value="de_DE"></s:url>'>Switch to German Locale</a>

The value will remain throughout the session. Still would not parse the
date manually in the action then.

- René

Greg Lindholm schrieb:
> Don't get me wrong, I think Struts does the best it can, the problem
> is many developers who have never written an I18N app seem to expect
> it to "just work" auto-magically and it doesn't.
> One of the issues is the only "automatic" handling relies on the
> browsers "accept-language" header.
> An experience shows that you just cannot rely on the accept-language
> header always being present or set correctly.
> 
> The developer may put a field on the screen and ask the user to enter
> a date yet the application may have no idea what locale that user is
> in and thus no idea what properly formatted date should look like.
> 
> The best approach for getting a date from the user (if you can't use a
> javascript control) is to always specify the format you are expecting.
> 
>     "Enter start date (mm/dd/yyyy): [       ]"
> 
> You can adjust the prompt based on the locale you "think" the user is
> in. So if you think the user is in Canada your en_CA bundle can
> specify the prompt like this:
> 
>     "Enter start date (dd/mm/yyyy): [       ]"
> 
> My main point is the developer needs to manage the Locale and let the
> user know what is expected and that struts will not handle it
> automatically.
> 
> 
> On Wed, Mar 31, 2010 at 7:21 AM, Rene Gielen <gi...@it-neering.net> wrote:
>> Hi,
>>
>> (copied from an earlier thread on numbers)
>>
>> see
>> http://struts.apache.org/2.1.8.1/docs/formatting-dates-and-numbers.html
>>
>> It is important to know that Struts 2 has always a locale context
>> request for each request, which affects both formating and parsing dates
>> and numbers. The request locale is determined as follows:
>>
>> 1. Request parameter
>> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
>> 2. Session attribute
>> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
>> 3. Browser preference
>> 4. Server side JVM default
>>
>> The first matching lookup wins.
>>
>> The type conversion mechanism tries to parse dates and numbers in the
>> current locale context, thus you should be fine when you ensure that you
>> have a consistent locale request setting both for the first request
>> (rendering the form and the current value in the textfield) and the
>> second request (processing the submitted form).
>>
>> - René
>>
>> Wick, Dan schrieb:
>>> I accept your answer, but Struts2 should just handle this! :-(
>>>
>>> I guess I'll write a number of helper methods like you did.
>>>
>>>> -----Original Message-----
>>>> From: Greg Lindholm [mailto:greg.lindholm@gmail.com]
>>>> Sent: Tuesday, March 30, 2010 10:14 AM
>>>> To: Struts Users Mailing List
>>>> Subject: Re: textfield with Date object
>>>>
>>>> (Seems like I just answered this question... but here it is again this
>>>> time for Dates)
>>>>
>>>> In my experience it's better to define number (and time) input fields
>>>> as String and perform the conversion and validation in your action
>>>> using java.text.NumberFormat (and java.text.DateFormat).  This puts
>>>> you in control of the
>>>> process and let's you handle I18N issues that way you want to without
>>>> fighting Struts.
>>>>
>>>> I typically have a couple of helper methods I put into the base Action
>>>> class that simplify getting the users Locale and DateFormat.
>>>> Depending on the app the users Locale may come from a Login object or
>>>> the browser request (which Struts can retrieve) or from a cookie.
>>>>
>>>> On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan <Da...@donaldson.com>
>>>> wrote:
>>>>> Does anyone know how to change the format of dates coming in on a
>>>> text input being backed by a java.util.Date in the Action?  If I enter
>>>> dates in US format (ex: 3/31/2010 for March 31, 2010) everything is
>>>> fine.  This requires a different style input though, using
>>>> Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse
>>>> it for a date myself, but I'm wondering if there's a Right way to do
>>>> it.
>>>>> --Dan
>>>>>
>>>>> <-- Snip of jsp page -->
>>>>> Date Of Birth*
>>>>> <s:textfield name="dob" required="true" tabindex="4" size="30"
>>>> cssClass="staticBody" cssErrorClass="formError"/>
>>>>> <-- End snip of jsp page -->
>>>>>
>>>>>
>>>>> <-- Snip of Action -->
>>>>>
>>>>>        private Date dob;
>>>>>        public Date getDob() {
>>>>>                return dob;
>>>>>        }
>>>>>        @RequiredStringValidator(message="Date of Birth is required.",
>>>> trim=true)
>>>>>        public void setDob(Date dob) {
>>>>>                this.dob = dob;
>>>>>        }
>>>>> <-- End snip of Action -->
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> 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
>>>
>> --
>> René Gielen
>> IT-Neering.net
>> Saarstrasse 100, 52062 Aachen, Germany
>> Tel: +49-(0)241-4010770
>> Fax: +49-(0)241-4010771
>> Cel: +49-(0)163-2844164
>> http://twitter.com/rgielen
>>
>> ---------------------------------------------------------------------
>> 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
> 

-- 
René Gielen
IT-Neering.net
Saarstrasse 100, 52062 Aachen, Germany
Tel: +49-(0)241-4010770
Fax: +49-(0)241-4010771
Cel: +49-(0)163-2844164
http://twitter.com/rgielen

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


Re: textfield with Date object

Posted by Greg Lindholm <gr...@gmail.com>.
Don't get me wrong, I think Struts does the best it can, the problem
is many developers who have never written an I18N app seem to expect
it to "just work" auto-magically and it doesn't.
One of the issues is the only "automatic" handling relies on the
browsers "accept-language" header.
An experience shows that you just cannot rely on the accept-language
header always being present or set correctly.

The developer may put a field on the screen and ask the user to enter
a date yet the application may have no idea what locale that user is
in and thus no idea what properly formatted date should look like.

The best approach for getting a date from the user (if you can't use a
javascript control) is to always specify the format you are expecting.

    "Enter start date (mm/dd/yyyy): [       ]"

You can adjust the prompt based on the locale you "think" the user is
in. So if you think the user is in Canada your en_CA bundle can
specify the prompt like this:

    "Enter start date (dd/mm/yyyy): [       ]"

My main point is the developer needs to manage the Locale and let the
user know what is expected and that struts will not handle it
automatically.


On Wed, Mar 31, 2010 at 7:21 AM, Rene Gielen <gi...@it-neering.net> wrote:
> Hi,
>
> (copied from an earlier thread on numbers)
>
> see
> http://struts.apache.org/2.1.8.1/docs/formatting-dates-and-numbers.html
>
> It is important to know that Struts 2 has always a locale context
> request for each request, which affects both formating and parsing dates
> and numbers. The request locale is determined as follows:
>
> 1. Request parameter
> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
> 2. Session attribute
> (http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
> 3. Browser preference
> 4. Server side JVM default
>
> The first matching lookup wins.
>
> The type conversion mechanism tries to parse dates and numbers in the
> current locale context, thus you should be fine when you ensure that you
> have a consistent locale request setting both for the first request
> (rendering the form and the current value in the textfield) and the
> second request (processing the submitted form).
>
> - René
>
> Wick, Dan schrieb:
>> I accept your answer, but Struts2 should just handle this! :-(
>>
>> I guess I'll write a number of helper methods like you did.
>>
>>> -----Original Message-----
>>> From: Greg Lindholm [mailto:greg.lindholm@gmail.com]
>>> Sent: Tuesday, March 30, 2010 10:14 AM
>>> To: Struts Users Mailing List
>>> Subject: Re: textfield with Date object
>>>
>>> (Seems like I just answered this question... but here it is again this
>>> time for Dates)
>>>
>>> In my experience it's better to define number (and time) input fields
>>> as String and perform the conversion and validation in your action
>>> using java.text.NumberFormat (and java.text.DateFormat).  This puts
>>> you in control of the
>>> process and let's you handle I18N issues that way you want to without
>>> fighting Struts.
>>>
>>> I typically have a couple of helper methods I put into the base Action
>>> class that simplify getting the users Locale and DateFormat.
>>> Depending on the app the users Locale may come from a Login object or
>>> the browser request (which Struts can retrieve) or from a cookie.
>>>
>>> On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan <Da...@donaldson.com>
>>> wrote:
>>>> Does anyone know how to change the format of dates coming in on a
>>> text input being backed by a java.util.Date in the Action?  If I enter
>>> dates in US format (ex: 3/31/2010 for March 31, 2010) everything is
>>> fine.  This requires a different style input though, using
>>> Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse
>>> it for a date myself, but I'm wondering if there's a Right way to do
>>> it.
>>>> --Dan
>>>>
>>>> <-- Snip of jsp page -->
>>>> Date Of Birth*
>>>> <s:textfield name="dob" required="true" tabindex="4" size="30"
>>> cssClass="staticBody" cssErrorClass="formError"/>
>>>> <-- End snip of jsp page -->
>>>>
>>>>
>>>> <-- Snip of Action -->
>>>>
>>>>        private Date dob;
>>>>        public Date getDob() {
>>>>                return dob;
>>>>        }
>>>>        @RequiredStringValidator(message="Date of Birth is required.",
>>> trim=true)
>>>>        public void setDob(Date dob) {
>>>>                this.dob = dob;
>>>>        }
>>>> <-- End snip of Action -->
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> 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
>>
>
> --
> René Gielen
> IT-Neering.net
> Saarstrasse 100, 52062 Aachen, Germany
> Tel: +49-(0)241-4010770
> Fax: +49-(0)241-4010771
> Cel: +49-(0)163-2844164
> http://twitter.com/rgielen
>
> ---------------------------------------------------------------------
> 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


Re: textfield with Date object

Posted by Rene Gielen <gi...@it-neering.net>.
Hi,

(copied from an earlier thread on numbers)

see
http://struts.apache.org/2.1.8.1/docs/formatting-dates-and-numbers.html

It is important to know that Struts 2 has always a locale context
request for each request, which affects both formating and parsing dates
and numbers. The request locale is determined as follows:

1. Request parameter
(http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
2. Session attribute
(http://struts.apache.org/2.1.8/docs/i18n-interceptor.html)
3. Browser preference
4. Server side JVM default

The first matching lookup wins.

The type conversion mechanism tries to parse dates and numbers in the
current locale context, thus you should be fine when you ensure that you
have a consistent locale request setting both for the first request
(rendering the form and the current value in the textfield) and the
second request (processing the submitted form).

- René

Wick, Dan schrieb:
> I accept your answer, but Struts2 should just handle this! :-(
> 
> I guess I'll write a number of helper methods like you did.
> 
>> -----Original Message-----
>> From: Greg Lindholm [mailto:greg.lindholm@gmail.com]
>> Sent: Tuesday, March 30, 2010 10:14 AM
>> To: Struts Users Mailing List
>> Subject: Re: textfield with Date object
>>
>> (Seems like I just answered this question... but here it is again this
>> time for Dates)
>>
>> In my experience it's better to define number (and time) input fields
>> as String and perform the conversion and validation in your action
>> using java.text.NumberFormat (and java.text.DateFormat).  This puts
>> you in control of the
>> process and let's you handle I18N issues that way you want to without
>> fighting Struts.
>>
>> I typically have a couple of helper methods I put into the base Action
>> class that simplify getting the users Locale and DateFormat.
>> Depending on the app the users Locale may come from a Login object or
>> the browser request (which Struts can retrieve) or from a cookie.
>>
>> On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan <Da...@donaldson.com>
>> wrote:
>>> Does anyone know how to change the format of dates coming in on a
>> text input being backed by a java.util.Date in the Action?  If I enter
>> dates in US format (ex: 3/31/2010 for March 31, 2010) everything is
>> fine.  This requires a different style input though, using
>> Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse
>> it for a date myself, but I'm wondering if there's a Right way to do
>> it.
>>> --Dan
>>>
>>> <-- Snip of jsp page -->
>>> Date Of Birth*
>>> <s:textfield name="dob" required="true" tabindex="4" size="30"
>> cssClass="staticBody" cssErrorClass="formError"/>
>>> <-- End snip of jsp page -->
>>>
>>>
>>> <-- Snip of Action -->
>>>
>>>        private Date dob;
>>>        public Date getDob() {
>>>                return dob;
>>>        }
>>>        @RequiredStringValidator(message="Date of Birth is required.",
>> trim=true)
>>>        public void setDob(Date dob) {
>>>                this.dob = dob;
>>>        }
>>> <-- End snip of Action -->
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
> 

-- 
René Gielen
IT-Neering.net
Saarstrasse 100, 52062 Aachen, Germany
Tel: +49-(0)241-4010770
Fax: +49-(0)241-4010771
Cel: +49-(0)163-2844164
http://twitter.com/rgielen

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


RE: textfield with Date object

Posted by "Wick, Dan" <Da...@Donaldson.com>.
I accept your answer, but Struts2 should just handle this! :-(

I guess I'll write a number of helper methods like you did.

> -----Original Message-----
> From: Greg Lindholm [mailto:greg.lindholm@gmail.com]
> Sent: Tuesday, March 30, 2010 10:14 AM
> To: Struts Users Mailing List
> Subject: Re: textfield with Date object
> 
> (Seems like I just answered this question... but here it is again this
> time for Dates)
> 
> In my experience it's better to define number (and time) input fields
> as String and perform the conversion and validation in your action
> using java.text.NumberFormat (and java.text.DateFormat).  This puts
> you in control of the
> process and let's you handle I18N issues that way you want to without
> fighting Struts.
> 
> I typically have a couple of helper methods I put into the base Action
> class that simplify getting the users Locale and DateFormat.
> Depending on the app the users Locale may come from a Login object or
> the browser request (which Struts can retrieve) or from a cookie.
> 
> On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan <Da...@donaldson.com>
> wrote:
> > Does anyone know how to change the format of dates coming in on a
> text input being backed by a java.util.Date in the Action?  If I enter
> dates in US format (ex: 3/31/2010 for March 31, 2010) everything is
> fine.  This requires a different style input though, using
> Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse
> it for a date myself, but I'm wondering if there's a Right way to do
> it.
> >
> > --Dan
> >
> > <-- Snip of jsp page -->
> > Date Of Birth*
> > <s:textfield name="dob" required="true" tabindex="4" size="30"
> cssClass="staticBody" cssErrorClass="formError"/>
> > <-- End snip of jsp page -->
> >
> >
> > <-- Snip of Action -->
> >
> >        private Date dob;
> >        public Date getDob() {
> >                return dob;
> >        }
> >        @RequiredStringValidator(message="Date of Birth is required.",
> trim=true)
> >        public void setDob(Date dob) {
> >                this.dob = dob;
> >        }
> > <-- End snip of Action -->
> >
> >
> >
> > ---------------------------------------------------------------------
> > 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


Re: textfield with Date object

Posted by Greg Lindholm <gr...@gmail.com>.
(Seems like I just answered this question... but here it is again this
time for Dates)

In my experience it's better to define number (and time) input fields
as String and perform the conversion and validation in your action
using java.text.NumberFormat (and java.text.DateFormat).  This puts
you in control of the
process and let's you handle I18N issues that way you want to without
fighting Struts.

I typically have a couple of helper methods I put into the base Action
class that simplify getting the users Locale and DateFormat.
Depending on the app the users Locale may come from a Login object or
the browser request (which Struts can retrieve) or from a cookie.

On Tue, Mar 30, 2010 at 10:34 AM, Wick, Dan <Da...@donaldson.com> wrote:
> Does anyone know how to change the format of dates coming in on a text input being backed by a java.util.Date in the Action?  If I enter dates in US format (ex: 3/31/2010 for March 31, 2010) everything is fine.  This requires a different style input though, using Day/Month/Year (31/3/2010).  I suppose I could take in a string & parse it for a date myself, but I'm wondering if there's a Right way to do it.
>
> --Dan
>
> <-- Snip of jsp page -->
> Date Of Birth*
> <s:textfield name="dob" required="true" tabindex="4" size="30" cssClass="staticBody" cssErrorClass="formError"/>
> <-- End snip of jsp page -->
>
>
> <-- Snip of Action -->
>
>        private Date dob;
>        public Date getDob() {
>                return dob;
>        }
>        @RequiredStringValidator(message="Date of Birth is required.", trim=true)
>        public void setDob(Date dob) {
>                this.dob = dob;
>        }
> <-- End snip of Action -->
>
>
>
> ---------------------------------------------------------------------
> 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