• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Dealing with Date Inputs

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for your reply. I'll try that and see how it goes!


Jamie
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic