.properties文件中的Bean验证和错误消息 [英] Bean Validation and error messages at .properties file
问题描述
// 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屋!