使用胸腺生长因子
Person.java
public class Person {
@NotEmpty(message="{Valid.Password}")
private String password;
}message.properties
Valid.Password = Password is Empty!!Login.html
<span class="error" th:errors="person.password"></span>th:errors无法检索显示为空区域的“Valid.Password”消息。
如果消息密钥更改为NotEmpty.person.password of message.properties,则它正在工作。
如何使用自定义消息密钥?
发布于 2013-12-06 11:34:13
使用类上的javax.validation约束和添加自定义消息需要基本上覆盖默认消息。例如:
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
@Size(min=17, max=17, message="VIN number must have 17 characters")
private String vin;
@NotEmpty
@Pattern(regexp="[a-z]*", flags = Pattern.Flag.CASE_INSENSITIVE)
private String make;
@NotEmpty
@Pattern(regexp="[a-z]*", flags = Pattern.Flag.CASE_INSENSITIVE)
private String model;若要重写默认值,请在验证本身中插入自定义消息,如VIN,或将它们添加到messages.properties文件中:
# Validation messages
notEmpty.message = The value may not be empty!
notNull.message = The value cannot be null!通过这种方式,您将重写以下的默认值:
javax.validation.constraints.NotEmpty.message
javax.validation.constraints.NotNull.message如果您需要针对不同字段的特定消息,则您的邮件属性应该以以下格式包含它们: constraintName.Class.field。例如:
NotEmpty.Vehicle.model = Model cannot be empty!如果不包括上面这样的重写值,它将显示默认值,或者显示一般覆盖(如notEmpty消息)。
当然,还有其他显示验证消息的方法,比如使用来自Validator实例的ConstraintViolation对象的信息。
希望这能帮上忙
https://stackoverflow.com/questions/20323655
复制相似问题