0

I have a model object with an XMLGregorianCalendar field. How can I bind it to an input field?

For string fields I'm using:

#springFormInput("model.object.stringfield" "")

but can't work out the corresponding code for an XMLGregorianCalendar

skaffman
  • 398,947
  • 96
  • 818
  • 769
Mark Pope
  • 11,244
  • 10
  • 49
  • 59
  • Well, what do you expect from it, to automatically turn into some sort of date picker? :) – serg Mar 22 '10 at 20:51
  • No, I expect it to bind an input field with a given date format to the XMLGregorianCalendar field... – Mark Pope Nov 04 '10 at 11:20

2 Answers2

0

You may be better off converting the XMLGregorianCalendar to something easier to handle like Calendar or Date before handing it off to the presentation layer.

Community
  • 1
  • 1
Nicholas Trandem
  • 2,815
  • 5
  • 30
  • 32
  • The XMLGregorianCalendar field is in a JAXB generated class which is used as the model so there's no scope for converting – Mark Pope Nov 04 '10 at 11:03
0

Here's a solution. It uses jodatime but could probably be changed not to:

For the view (velocity in this case):

#springFormInput("model.object.xmlgregoriancalendar.field" "")

For the controller:

@InitBinder
public void binder(WebDataBinder binder) {
    binder.registerCustomEditor(XMLGregorianCalendar.class, new PropertyEditorSupport() {
       public void setAsText(String value) {
           setValue(createXMLGregorianCalendar(value));
        }

        public String getAsText() {
            return new SimpleDateFormat("dd/MM/yyyy").format(((XMLGregorianCalendar)getValue()).toGregorianCalendar().getTime());
        }  
    });
}

private XMLGregorianCalendar createXMLGregorianCalendar(String date) {
    LocalDateTime result = DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(date).toLocalDateTime();
    return xmlDF().newXMLGregorianCalendar(result.toDateTime().toGregorianCalendar());
}


private static DatatypeFactory xmlDF() {
    try {
        return DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException ex) {
        throw new RuntimeException(ex);
    }
}
Mark Pope
  • 11,244
  • 10
  • 49
  • 59