I wonder if anyone can help. I’m writing a Spring MVC / Webflow web app where I need to collect data from the user. I have a form backing object which mirrors the backend schema (I think JAXB was used to generate the classes). Anyway, one of the fields I need to capture is DOB, which in the form backing object is a single property of type XMLGregorianCalendar
For the most part, html input fields on the form are a 1 to 1 mapping to the property in the form backing object – eg: the html input “surname” maps to the String property called “surname” in the form backing object (via the getters and setters). Nice 🙂
But date fields are a bit different. In the form backing object, it is only one property with one getter/setter pair; but on screen we might want to present it as 3 seperate fields – perhaps a drop down for Day, a dropdown for Month and a text box for Year
This type of problem must come up in every web app, regardless of backend technology, and I’m wondering how other people deal with it.
I’ve got two thoughts:
1) extend my form backing object so that I have properties for dobDay, dobMonth and dobYear, then as part of my validator take the 3 values and set the real DOB property from them. I’ve done this in the past, it kind of works, but if feels a bit rubbish, especially if the web app has lots of dates.
2) Find or write a jsp taglib that does this magic for me. I can see how this work work when rendering the form – the taglib can be called with the property name in the FBO, it can get the current date value, then can write to the output 3 html fields. But I cant see how the submission would work – something would need to read the 3 html fields and ‘glue’ them into the real property in the FBO
So, really, its just a call for help – what have other people done with this kind of thing in the past?
Appreciate your thoughts
Nathan
You could use either option, but I tend to agree with you that it isn’t very slick to have the command object contain the date components.
Number 2 will work if you add a hidden field in the page for the date field and you use the value to fill your three combos for day, month and year. On form submit you must first use javascript to reassemble the values from the combos inside this hidden field (which will map with the date field in the command object).
I think this is the cleanest way, but if you don’t want to use javascript, you could submit the three values (with no correspondent fields in the command object) and assemble them after your command is bind.
You don’t mention the Spring version you are using but for 2.x you could intervene in the workflow on the
onBindmethod to reassemble the values there.Things changed in Spring 3 and the controller classes are deprecated in favor of annotated controllers but you might get a similar result if you use an InitBinder in combination with the request to register a custom date editor and reassemble the fields; for example:
Command:
Controller:
Form:
Form result:
Property editor: