使用自定义消息进行 JSR-303 验证

发布于 2024-11-02 07:57:02 字数 1128 浏览 7 评论 0原文

我正在使用 Spring 3.0.5-RELEASE,带有 JSR-303 样式验证和 Hibernate 验证器 4.1.0-Final。我的模型类看起来像这样:

public class Model {
    @Max(value=10,message="give a lower value")
    Integer n;
}

这在带有绑定的 spring mvc servlet 中作为请求参数传递。所以请求看起来像这样:

http://localhost:8080/path?n=10

我想要的是当出现类型不匹配异常时能够自定义错误消息,例如

http://localhost:8080/path?n=somestring

这会导致我想要替换的很长的默认消息。

我已经尝试了网络上描述的几乎所有配置,但似乎都不起作用。有人知道正确的配置是什么吗?

具体来说,我的 mvc-servlet.xml 中需要什么? messages.properties 文件中需要什么? message.properties 文件是否有一个神奇的名称,以便 hibernate-validator 能够找到它?

我在 mvc-servlet.xml 中使用了以下内容,但没有成功:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:basename="messages" />

以及 src/main/resources 中的 messages.properties 文件(以及 src/main/webapp/WEB-INF 中的 messages.properties 文件)...

我已经尝试了所有方法messages.properties 中的各种组合,甚至可以简单地覆盖 @NotEmpty 消息,甚至这对我来说也不起作用。

I'm using Spring 3.0.5-RELEASE, with JSR-303 style validation and Hibernate validator 4.1.0-Final. My model class looks something like this:

public class Model {
    @Max(value=10,message="give a lower value")
    Integer n;
}

And this is passed as request param in a spring mvc servlet with binding. So the request would look something like this:

http://localhost:8080/path?n=10

What I want is to be able to customize the error message when there is a type mismatch exception, e.g.

http://localhost:8080/path?n=somestring

Which results in a very long default message that I want to replace.

I've tried just about every configuration described on the web and none of them seem to work. Does someone know what the right configuration is?

Specifically, what do I need in my mvc-servlet.xml? What do I need in my messages.properties file? Does the message.properties file have a magic name so that hibernate-validator will find it?

I've used the following in my mvc-servlet.xml without success:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" p:basename="messages" />

And a messages.properties file at src/main/resources (and also at src/main/webapp/WEB-INF)...

I've tried all sorts of combinations in messages.properties even for doing simple override of say @NotEmpty messages and even that doesn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只有一腔孤勇 2024-11-09 07:57:02

错误消息需要放入名为 ValidationMessages.properties 的文件中。只需将其放在类路径中的 hibernate jar 之前的某个位置即可。

例如,如果您有一个包含以下属性的 ValidationMessages.properties 文件:

email_invalid_error_message=Sorry you entered an invalid email address
email_blank_error_message=Please enter an email address

那么您可以拥有一个具有以下约束的域对象:

public class DomainObject{ 
...
@Email(message = "{email_invalid_error_message}")
@NotNull(message = "{email_blank_error_essage}")
@Column(unique = true, nullable = false)
String primaryEmail;
...
}

Error messages need to go into a file named ValidationMessages.properties. Just put that somewhere in your classpath before the hibernate jars.

For example if you have a ValidationMessages.properties file that contains the following properties:

email_invalid_error_message=Sorry you entered an invalid email address
email_blank_error_message=Please enter an email address

Then you could have a domain object with the following constraints:

public class DomainObject{ 
...
@Email(message = "{email_invalid_error_message}")
@NotNull(message = "{email_blank_error_essage}")
@Column(unique = true, nullable = false)
String primaryEmail;
...
}
葮薆情 2024-11-09 07:57:02

我在另一个问题中找到了答案,它对我有用:
spring 3.0 MVC 似乎忽略 messages.properties

由于我是 Spring 的新手,令我困惑的是我希望能够将消息放入 ValidationMessages.properties 中。但是,此错误发生在验证之前的绑定时。因此,不要使用 ValidationMessages.properties,而是使用常规 ResourceBundle messages.properties 文件。

将其放入 ??-servlet.xml 文件中:

<bean id="messageSource" 
 class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/> </bean>

将此类行放入 messages.properties 文件中。

typeMismatch.myRequest.amount=Amount should be a number.
methodInvocation.myRequest.amount=Amount should be a number.

在此示例中,myRequest 是我的表单模型,amount 是该表单中的一个属性。
typeMismatch 和 methodInitation 是预定义的,对应于绑定/转换异常,就像您似乎得到的那样。我很难为它们找到好的文档或值列表,但我发现它们对应于一些 Spring 异常中的 ERROR_CODE 常量。 http: //static.springsource.org/spring/docs/2.5.x/api/constant-values.html#org.springframework.beans.MethodInitationException.ERROR_CODE

我将此项目的 messages.properties 文件放在根目录中我的源文件夹,将其与 ValidationMessages.properties 一起放入类路径的根目录中。

I found an answer to this in another question and it worked for me:
spring 3.0 MVC seems to be ignoring messages.properties

The confusing part to me, as I am new to Spring, is that I expected to be able to put the message in ValidationMessages.properties. However, this error happens at binding time, before validation. So instead of using ValidationMessages.properties, use your regular ResourceBundle messages.properties file.

Put this in your ??-servlet.xml file:

<bean id="messageSource" 
 class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/> </bean>

Put lines like these in your messages.properties file.

typeMismatch.myRequest.amount=Amount should be a number.
methodInvocation.myRequest.amount=Amount should be a number.

In this example myRequest is my form model and amount is a property in that form.
typeMismatch and methodInvocation are pre-defined and correspond to binding/conversion exceptions like the one you seem to be getting. I had a hard time finding good documentation for them or a list of values, but I found that they correspond to ERROR_CODE constants in some Spring exceptions. http://static.springsource.org/spring/docs/2.5.x/api/constant-values.html#org.springframework.beans.MethodInvocationException.ERROR_CODE

I put my messages.properties file for this project in the root of my source folder, which puts it in the root of my classpath, along with ValidationMessages.properties.

心清如水 2024-11-09 07:57:02

我想要的是能够在出现类型不匹配异常时自定义错误消息,例如

据我了解,此消息是由 Spring MVC 生成的,而不是由 Hibernate Validator 生成的。我建议将以下行添加到 messages.properties 文件中:

typeMismatch.java.lang.Integer=Must specify an integer value

What I want is to be able to customize the error message when there is a type mismatch exception, e.g.

As I understand this messages generated by Spring MVC, not by Hibernate Validator. I suggest to add following line to the messages.properties file:

typeMismatch.java.lang.Integer=Must specify an integer value
怀念你的温柔 2024-11-09 07:57:02

问题是针对数据绑定错误消息,而不是针对验证错误消息。有一个足够的答案这里

The question is for databinding error messages and not for validation error messages. There is an adequate answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文