Spring MVC- Joda datetime错误消息无效日期 [英] Spring MVC- Joda datetime error message for invalid date

查看:146
本文介绍了Spring MVC- Joda datetime错误消息无效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的请求对象中有一个joda datetime 属性,我已经使用了 @DateTimeFormat 作为日期格式化。

但是当我输入无效的日期(比如说为55天)时,会显示整个消息

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 date(say day as 55), it displays the whole message

无法转换属性值键入java.lang.String为必需类型org.joda.time.DateTime为属性request.myDate;嵌套异常是org.springframework.core.convert.ConversionFailedException:无法将值55/12/2014从类型java.lang.String转换为类型org.joda.time.DateTime;嵌套异常是org.joda.time.IllegalFieldValueException:无法解析55/12/2014:dayOfMonth的值55必须在范围[1,31]

如何将此消息由Spring生成,如何将其更改为自定义消息。

How can I change it to a custom message as the present message is generated by Spring.

在另一个SO问题中

Spring mvc如果无效日期,Joda Datetime转换器失败

要求在message.properties中输入。

但是我无法理解如何从属性文件中选择邮件,因为我没有执行任何 error.rejectValue 类型的事物,那么消息如何被识别。

In another SO question
Spring mvc Joda Datetime converter fail if invalid date
it is asked to make entry in message.properties.
But I'm not able to understand how will it pick the message from property file as I am not doing any error.rejectValue type of thing, So how will the message be identified.

推荐答案

您在问题中链接的方法应该可以工作。但您应该使用的消息密钥是例如 typeMismatch.org.joda.time.DateTime

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.

即使您不会手动拒绝该值,因为Spring将根据JavaDoc DefaultMessageCodesResolver

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.

在这种情况下,Spring将会特别寻找以下代码:

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


  1. typeMismatch.request.myDate - 名为request的对象名为myDate的转化消息

  2. typeMismatch.myDate - 转换消息属性名为myDate

  3. typeMismatch.org.joda.time.DateTime - DateTime类型的转换消息

  4. typeMismatch - 常规转换​​错误消息

  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

所以你可以定义一些一般的 typeMismatch 消息,如格式不正确然后根据需要定义更多的特定消息。更具体的错误代码的优先级高于下面的错误代码。因此,如果您同时定义了 typeMismatch typeMismatch.org.joda.time.DateTime 消息,那么后者将被使用。

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.

这篇关于Spring MVC- Joda datetime错误消息无效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆