I have mad开发者_JAVA技巧e the Date in in my java Class
and this is the code i have used in controller
@InitBinder
public void initBinder(final WebDataBinder binder) {
binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd-MM-yyyy"), true));
}
i am usinng JSR annotations and hibernate to validate other fields.
Is there any way i can use annotations to validate that date must in dd-mm-yyyy format only
The CustomDateEditor is not a validator itself, but in this case it does implicitly validate your pattern: it will just parse a string to a date using the format you specified. So you'll get a null value if the parsing does not succeed.
Spring validation occurs after binding, so any validation will be performed on the Date object (so after that string is parsed), not on the initial string.
精彩评论