Hey guys,
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:
<stripes:text
id="dateOrdered.date"
name="dateOrdered"
size="9"
formatType="date"
formatPattern="yyyy-MM-dd"
/>
<stripes:text
id="dateOrdered.time"
name="dateOrdered"
size="9"
formatType="time"
formatPattern="HH:mm:ss"
/>
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.
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.
Cheers,
Nathan
---- Start steps ---
a)
Implement a DateTimeView object that looks roughly like below:
import java.util.Date;
public class DateTimeView {
private Date date;
private Date time;
//getters/setters omitted for brevity
public Date toDate() {
return new Date(date.getTime() + time.getTime());
}
}
b) Extend the DateTypeConverter and add the following format Strings, so that
fields with just time can be converted to java.util.Date (date portion becomes
01/01/1970):
"HH:mm:ss.SSS",
"HH:mm:ss",
"HH:mm"
c) Extend the DefaultTypeConverterFactory so I can add my DateTypeConverter for
java.util.Date
d) Register my custom TypeConverterFactory in web.xml
e) On beans that need time and date portion, use the DateTimeView, e.g.
DateTimeView dateOrdered;
f) Change form to use nested objects:
<stripes:text
id="dateOrdered.date"
name="dateOrdered.date"
size="9"
formatType="date"
formatPattern="yyyy-MM-dd"
/>
<stripes:text
id="dateOrdered.time"
name="dateOrdered.time"
size="9"
formatType="time"
formatPattern="HH:mm:ss"
/>
g) Code that needs to use the combined date object uses DateTimeView#toDate()
e.g.
service.findDeliveriesReceivedAfter(dateOrdered.toDate())
---- End steps ---
-------------------------------------------------------------------------
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