1

I am trying to bind string input form parameter to the date property of a bean using following code. I want to bind the date formatter only to a specific property named registrationDate of the bean User. I have tried various ways to specify the field name registrationDate in a call to binder.registerCustomEditor but CustomDateEditor never get's called.

CustomDateEditor works only if i drop the field name from binder.registerCustomEditor call and make it global for all date fields as demonstrated by the last commented line in the initBinderAll() method in the controller below. Hope someone points me in the right direction.

user.jsp :-

<form:form action="/some_url" modelAttribute="user">
    <input type="text" id="registrationDate" name="registrationDate" value="${regDate}" />
</form:form>      

User.java :-

@Entity
@Table(name="user")
public class User {
    @Column(name = "reg_date")
    @Temporal(TemporalType.DATE)
    private Date registrationDate;
}   

UserController.java :-

@Controller
public class UserController {
    @RequestMapping(value = "/some_url", method = RequestMethod.POST)
    public String handleSubmission(
            @ModelAttribute("user")@Valid User user, BindingResult result) throws IOException {
    }

    @InitBinder
    public void initBinderAll(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, "user.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        binder.registerCustomEditor(Date.class, "User.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        binder.registerCustomEditor(Date.class, "registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        //binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), true));
    }
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
ivish
  • 572
  • 11
  • 35
  • I cannot replicate your error. `binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor( dateFormat, false));` works ok for me. The only thing rhat I can think of what might be causing this is if **Note: Only one single registered custom editor per property path is supported. In the case of a Collection/array, do not register an editor for both the Collection/array and each element on the same property.** from docs – ArunM Jan 31 '16 at 06:01
  • Hello Arun, unfortunately it's not working in my case. And there is only one custom editor for property 'registrationDate' in my case. The content of initBinderAll() method is exactly what i posted in the OP. – ivish Jan 31 '16 at 06:56
  • Have you tryed the annotation based way with `@DateTimeFormat` that use the Type Conversion API introduced in Spring 3.0, insted of the old PropertyEditor. – Ralph Jan 31 '16 at 07:43
  • Thanks a lot Ralph. It works. You saved me a lot of trouble. If you can post your comment as an answer i will go ahead and accept it. – ivish Jan 31 '16 at 08:44

0 Answers0