开发者

How to validate date using Custom date proprty editor in spring MVC

开发者 https://www.devze.com 2023-02-15 17:00 出处:网络
I have mad开发者_JAVA技巧e the Date in in my java Class and this is the code i have used in controller

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消