Spring Confirm MVC Date Format with JSR303

I am using Spring MVC with JSR303 to validate input.

The form I created has a couple of date fields attached to the objects Datein the object that supports the form. I am using JSR303 to check for Dateusing @Future. I also use @DateTimeFormat(pattern="dd/MM/yyyy"), (I know this is not a check).

How to check the date format on Stringa form? If I leave the other required fields blank ( @NotEmpty) and enter an invalid date in the form "dd / MM / yy", it will be converted to "dd / MM / yyyy" when resubmitted (for example, 12/03/12 revised as 12/03 / 0012). This means that I will receive Duff data on my system. If I enter "aaa", I get a conversion exception. Correctly formatted is Stringconverted to Dateobjects.

In addition, the required annotation for the required field for fields Dateis equal to @NotNullor @NotEmpty?

Many thanks in advance for the advice provided.

+1
source share
2 answers

. ( ):

    @InitBinder
    public void initBinder(WebDataBinder binder) {


    String format = "dd/MM/yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    dateFormat.setLenient(false);
    CustomDateEditor customDateEditor = new CustomDateEditor(dateFormat,true,format.length());

    binder.registerCustomEditor(Date.class, customDateEditor);
    }

, :

typeMismatch.java.util.Date: - ,

, , .

+2

JSR303, ( baching) .

, , , , spring.

@See Spring : 6.5 Spring 3

+1

All Articles