@Notblank注释未显示消息



我试图显示一条消息,因为当表单中的字段为空时,我有@Size和@url,显示它们的消息,但我无法显示@Notblank注释消息。

变量的JPA代码是

@Basic(optional = false)
@NotNull
@Column(name = "country")
@NotBlank (message = " field cannot be empty")
private String country;

表单上显示错误的代码是

<tr>
<td><form:label path="country">Country</form:label></td>
<td><form:input path="country"/></td>
<td style="color:red"> <form:errors path="country"/> </td>
</tr>

我使用的依赖项是

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

我发现我使用了错误的导入

我在用

import javax.validation.constraints.NotBlank;

我应该在什么时候使用

import org.hibernate.validator.constraints.NotBlank;

也许问题是@NotNull注释,它在@NotBlank之前执行。

@NotNull:受约束的CharSequence、Collection、Map或Array只要不是null就有效,但它可以是空的

@NotEmpty:受约束的CharSequence、Collection、Map或Array只要不是null并且其大小/长度大于零,就有效

@NotBlank:一个受约束的字符串是有效的,只要它不是null并且修剪的长度大于零

最新更新