【发布时间】:2018-01-23 08:02:01
【问题描述】:
我无法解决我的验证消息。
我已经通过网络搜索和阅读了几个小时,我想将这个问题与Customize spring validation error的标记答案联系起来
我确实定义了一个 MessageSource bean,并且 messages.properties 它被正确读取,因为我还将它用于与 th:text="#{some.prop.name} 一起显示的常规文本,这确实工作得很好.
只是验证错误无法按应有的方式工作。
我确定这是一个愚蠢的错误,我只是忽略了...
验证本身工作正常。
约束:
@NotEmpty(message="{validation.mail.notEmpty}")
@Email()
private String mail;
messages.properties:
# Validation
validation.mail.notEmpty=The mail must not be empty!
模板部分:
<span th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}"></span>
显示的文字:
{validation.mail.notEmpty}
我尝试了很多变化,但都没有成功。
@NotEmpty(message="validation.mail.notEmpty")
@NotEmpty(message="#{validation.mail.notEmpty}")
将只显示消息字符串的确切值,不解析。
<span th:if="${#fields.hasErrors('mail')}" th:errors="${mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{*{mail}}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{__*{mail}__}"></span>
会导致错误。
编辑:
调试后,我偶然发现了这个:
班级:org.springframework.context.support.MessageSourceSupport
方法:formatMessage(String msg, Object[] args, Locale locale)
将被调用
formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)
它会遇到if (messageFormat == INVALID_MESSAGE_FORMAT) {
所以...我的消息格式不正确。这超出了我的范围/知识。有谁知道这是什么意思?
【问题讨论】:
标签: java spring spring-boot thymeleaf