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));
}
}