Spring MVC- Joda datetime error message for invalid date

后端 未结 1 1908
失恋的感觉
失恋的感觉 2020-12-21 16:00

I have a joda datetime attribute in my request object and I have used @DateTimeFormat for the date formatting.
But when I enter an invalid dat

相关标签:
1条回答
  • 2020-12-21 16:34

    The approach you linked to in your question should work. But the message key you should use is for instance typeMismatch.org.joda.time.DateTime.

    Even though you are not manually rejecting the value anywhere Spring will automatically know where to look for the message based on the rules described in the JavaDoc of DefaultMessageCodesResolver.

    In the situation you describe Spring will look for following codes specifically:

    1. typeMismatch.request.myDate - Conversion message for attribute named "myDate" on an object named "request"
    2. typeMismatch.myDate - Conversion message for attribute named myDate
    3. typeMismatch.org.joda.time.DateTime - Conversion message for DateTime type
    4. typeMismatch - General conversion error message

    So you can define some general typeMismatch message like Incorrect format and then define more specific messages as needed. The more specific error codes have higher priority than those below it. So if you have both typeMismatch and typeMismatch.org.joda.time.DateTime messages defined, the latter will be used.

    0 讨论(0)
提交回复
热议问题