Search code examples
javaspring-mvcpropertyeditor

Spring MVC : Binding 3 dropdowns to a date property in SimpleFormController


How should I configure the class to bind three dropdowns (date, month, year) to a single Date property so that it works the way it works for 'single request parameter per property' scenario ? I guess a should add some custom PropertyEditors by overriding initBinder method. What else ?


Solution

  • Aleksey Kudryavtsev: you can override the onBind method in your controller, i which you cant fiddle something special in command object, like

    dateField = new SimpleFormat("YYYY-mm-dd").parse(this.year + "-" + this.month + "-" this.day);
    

    or:

    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    dateField = calendar.getTime();
    

    but i'd rather do validation in javascript and use some available date picker component, there are plenty of them...