wood burning stoves 2.0*
The moose likes Struts and the fly likes Dealing with Date Inputs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Dealing with Date Inputs" Watch "Dealing with Date Inputs" New topic
Author

Dealing with Date Inputs

Jamie Jackson
Ranch Hand

Joined: Jan 11, 2005
Posts: 58
(Java/Struts Newbie)

I'm completely at a loss as to how to deal with date inputs in struts. I've got two fields on this form: startDate and endDate.

<tr>
<td>Start Date:</td>
<td><html:text property="startDate"/></td>
</tr>
<tr>
<td>End Date:</td>
<td><html:text property="endDate"/></td>
</tr>

In my action form, I've got the line:
BeanUtils.copyProperties( grantDTO, grantForm );

Which fails because these aren't simple properties.

Can anyone please give me an example of dealing with dates in struts, or give me a high-level explanation of how to do it?

Thanks,
Jamie
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Jamie,

The way I do it is to create a second getter and setter in the form bean called getFormattedStartDate() and setFormattedStartDate(), both of which deal in String datatypes.

In your getFormattedStartDate() method, use java.text.SimpleDateFormat to return a string display of the date in the format that you want.

In your setFormattedStartDate() method, you can use the parse() methof of SimpleDateFormat to parse the string representation of the date back into a java.util.Date

In your jsp, simply use <html:text property="formattedStartDate"/>

Merrill


Merrill
Consultant, Sima Solutions
Jamie Jackson
Ranch Hand

Joined: Jan 11, 2005
Posts: 58
Thanks so much for your reply. I'll try that and see how it goes!


Jamie
Gianluca Busan
Greenhorn

Joined: Feb 17, 2005
Posts: 1
You can also "reconfigure" completely the BeanUtils creating your converters and adding them also the "formatter" interface.
Convert means: from string to obj
Format means: from object to string
In this way you can configure the beanutils environment to automatically convert your xxxForm to xxxDTO and
format your xxxDTO to xxxForm.

It isn't a simple approach, but once you did it, it can be used for any DTO/Form without forcing you to create accessory get/set methods like getFormattedStartDate() or setFormattedStartDate() any time you have a Date field (or a Double/Integer....)
Jamie Jackson
Ranch Hand

Joined: Jan 11, 2005
Posts: 58
Okay, I went with the easy solution for now, and it works! The other is a little ambitious for me at the moment.

Thanks,
Jamie
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Dealing with Date Inputs