我遇到了一些我无法解决的问题。我想将JPA验证消息与Spring Thymeleaf进行交互。
我已经尝试了一些解决方案,但是都没有用。
这是我的实体类属性:
@Length(min = 1, message = "#{field.empty}")
@Valid
private String lastName;
这是百里香叶形式:
<div class="form-group col">
<label for="lastName" th:text="#{register.lastname}"></label>
<input id="lastName" class="form-control" type="text"
th:placeholder="#{register.lastname}"
th:field="*{lastName}"
th:classappend="${#fields.hasErrors('lastName')} ? is-invalid : ''"/>
<small class="form-text text-danger"
th:if="${#fields.hasErrors('lastName')}"
th:errors="*{lastName}"></small>
</div>
我希望以与您在此处看到的相同的方式打印已翻译的错误消息
<label for="lastName" th:text="#{register.lastname}"></label>
但是我看不到翻译的输出,而是看到#{field.empty}
有什么办法吗?
答案 0 :(得分:1)
因此,要感谢Springk的《守门员》(第8章:Spring JSR 303 Bean验证),我解决了我的问题。您可以在messages.property泛型中为注释类型和特定的类属性设置错误消息:
还有
@Length
使用
Length=error message here.
对于此特定字段(姓氏)
Length.lastName=error message here
所有JPA注释都可以使用