覆盖Spring表单错误消息 [英] Override Spring form error messages

查看:98
本文介绍了覆盖Spring表单错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring中,我如何覆盖默认的表单错误消息?

In Spring how do I override default form error massages ?

我正在使用 Validator 和一个属性文件中添加我自己的错误消息,但是
如何覆盖例如在转换/编码错误时打印的消息?

I'm using a Validator and a properties file to add my own error messages, but how do I override messages that get printed on conversion/encoding error for example ?

它们似乎是生成的自动,并且我认为对用户没有帮助:

They seem to be generated automatically and I don't think are helpful for the user:

Failed to convert property value of type java.lang.String to required type java.lang.Double for property minPrice; nested exception is java.lang.NumberFormatException: 


推荐答案

遵循Spring的 DefaultMessageCodeResolver 。为了完整起见,这里是其文档的相关部分:

You can override the defaults by creating custom messages in your localization bundle with keys following conventions defined by Spring's DefaultMessageCodeResolver. For the sake of completeness here is the relevant part of its documentation:


将为对象错误创建两个消息代码,在下面的$中b $ b顺序(使用前缀格式器时):

Will create two message codes for an object error, in the following order (when using the prefixed formatter):

1.: code + "." + object name
2.: code 

将为字段说明创建四个消息代码,按照
的顺序排列:

Will create four message codes for a field specification, in the following order:

1.: code + "." + object name + "." + field
2.: code + "." + field
3.: code + "." + field type
4.: code 

例如,在代码 typeMismatch的情况下,对象名称用户,字段
年龄:

For example, in case of code "typeMismatch", object name "user", field "age":

1. try "typeMismatch.user.age"
2. try "typeMismatch.age"
3. try "typeMismatch.int"
4. try "typeMismatch" 

因此,可以利用此解析算法来显示
特定消息,以绑定错误,例如 required和
typeMismatch:

This resolution algorithm thus can be leveraged for example to show specific messages for binding errors like "required" and "typeMismatch":

at the object + field level ("age" field, but only on "user");
at the field level (all "age" fields, no matter which object name);
or at the general level (all fields, on any object). 

对于数组,List或Map属性,特定
元素和整个集合都生成了。假设对象用户中数组组的字段
的名称为:

In case of array, List or Map properties, both codes for specific elements and for the whole collection are generated. Assuming a field "name" of an array "groups" in object "user":

1. try "typeMismatch.user.groups[0].name"
2. try "typeMismatch.user.groups.name"
3. try "typeMismatch.groups[0].name"
4. try "typeMismatch.groups.name"
5. try "typeMismatch.name"
6. try "typeMismatch.java.lang.String"
7. try "typeMismatch" 

默认情况下,错误代码将放置在
构造的消息字符串的开头。 messageCodeFormatter属性可以是
,用于指定其他串联格式。

By default the errorCodes will be placed at the beginning of constructed message strings. The messageCodeFormatter property can be used to specify an alternative concatenation format.

为了将所有代码分组到
资源包内的特定类别中,例如 validation.typeMismatch.name代替
默认的 typeMismatch.name,请考虑指定要应用
的前缀。

In order to group all codes into a specific category within your resource bundles, e.g. "validation.typeMismatch.name" instead of the default "typeMismatch.name", consider specifying a prefix to be applied.

这篇关于覆盖Spring表单错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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