.properties文件中的Bean验证和错误消息 [英] Bean Validation and error messages at .properties file

查看:284
本文介绍了.properties文件中的Bean验证和错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Glassfish合作开发JSF Projekt。我的验证效果很好,但我不会成为一个自定义的错误信息。

  // Class = User,package = devteam 
@NotEmpty @Pattern(。+ @。+ \\。[az] +)
private String emailAddress;

我的ValidationMessages.properties位于带有以下内容的WEB-INF文件夹中:

  devteam.User.emailAddress =无效的电子邮件地址

谢谢。

解决方案

这里有两个问题。首先是 ValidationMessages.properties 文件的位置。它必须位于类路径的根目录,因此将其移入 WEB-INF / classes 中。
您的第二个问题是消息键。例如,Pattern约束的默认消息密钥是 {javax.validation.constraints.Pattern.message} 。在你的情况下,你要在 @Pattern 注释中指定消息参数:

  @Pattern regexp =。+ @。+ \\。[az] +,message ={devteam.User.emailAddress})


i am working on a JSF Projekt with Glassfish. My validation works well but i dont become a custom error message.

//Class = User, package = devteam
@NotEmpty @Pattern(".+@.+\\.[a-z]+")
private String emailAddress;

My ValidationMessages.properties is in the WEB-INF folder with this content:

devteam.User.emailAddress=Invalid e-mail address

Thank you.

解决方案

You are having two problems here. First, the location of the ValidationMessages.properties file. It has to be in the root of the classpath, so move it into WEB-INF/classes Your second problems are the message keys. The default message key for the Pattern constraint for example is {javax.validation.constraints.Pattern.message}. In your case you want to specify the message parameter in the @Pattern annotation:

@Pattern(regexp=".+@.+\\.[a-z]+", message="{devteam.User.emailAddress}")

这篇关于.properties文件中的Bean验证和错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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