繁体   English   中英

Thymeleaf的JPA验证消息国际化

[英]JPA validation messages internationalization with Thymeleaf

我遇到了一些我无法解决的问题。 我想用Spring Thymeleaf来对JPA验证消息进行内部化。

我已经尝试了几种解决方案,但是都没有用。

这是我的实体类属性:

@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}

有什么办法吗?

因此,多亏了Goalkicker春季图书(第8章:Spring JSR 303 Bean验证),我才解决了我的问题。 您可以在messages.property泛型中为注释类型和特定的类属性设置错误消息:

而对于

@Length

采用

Length=error message here. 

对于此特定字段(lastName)

Length.lastName=error message here

相同的内容可用于所有JPA批注

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM