【问题标题】:Spring boot + Thymeleaf custom error messagesSpring boot + Thymeleaf 自定义错误信息
【发布时间】:2016-03-31 00:17:55
【问题描述】:

我正在将 spring boot 和 thymeleaf 用于 Web 应用程序。

我想自定义验证错误消息而不是使用默认消息。我发现这样做的方法是创建一个名为 messages.properties 的文件,其中包含约束和消息。我还发现我可以根据我的目标类创建自定义消息,并且我还可以使用基于此的国际化方法。

为了覆盖默认消息,我创建了自己的 messages.properties 文件:

Size.message=El tamño no es correcto
NotNull.message=No puede ser nulo
Min.message=No cumple con el minimo

我在课堂上设置了约束:

public class Person {

    @Size(min=2, max=30)
    private String name;

    @NotNull
    @Min(value = 18L)
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

这是我的表格:

<form action="#" th:action="@{/form}" th:object="${person}" method="post">

    <table>
        <tr>
            <td>Name:</td>
            <td><input type="text" th:field="*{name}"/></td>
            <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name error</td>
        </tr>
        <tr>
            <td>Age:</td>
            <td><input type="text" th:field="*{age}"/></td>
            <td th:if="${#fields.hasErrors('age')}" th:errors="*{age}">Name error</td>
        </tr>
        <tr>
            <td>
                <button type="submit">Enviar</button>
            </td>
        </tr>
    </table>

</form>

我将messages.properties放在资源文件夹中:

根据这些链接我做得正确:

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

How to include message.properties with thymeleaf

但它仍然没有显示我写的自定义错误消息。

有什么建议吗? 提前感谢您的帮助。

带有示例代码的 GitHub:https://MichaelKnight@github.com/MichaelKnight/ValidandoFormularios.git

【问题讨论】:

  • 你能做一个干净的构建和检查吗?有时,在清理和构建应用程序之前,对 Spring Boot 应用程序所做的更改可能不会反映。
  • @harshavmb 我刚刚做到了。没什么新鲜的:(

标签: spring-boot thymeleaf


【解决方案1】:

因为您使用的是 JSR-303 验证器,所以您应该将您的消息放入名为 ValidationMessages.properties 的文件中。 JSR-303 提供程序(在本例中为 hibernate-validator)默认查找此文件(根据规范)。

详情见:https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-message-interpolation.html

【讨论】:

  • 如果您将使用javax.validation.constraints.NotNull.message 而不是NotNull.message(在ValidationMessages.properties 中),它是否有效?
  • 现在我们正在讨论。以这种方式工作得很好。但这只能部分解决我的问题。我真正想做的是根据类为错误消息分配自定义值,如下所述:stackoverflow.com/questions/20323655/… 如果您需要不同字段的特定消息,您的消息属性应以以下格式包含它们:[constraintName.Class 。场地]。例如:NotEmpty.Vehicle.model = 模型不能为空!
  • 这里还解释了如何以另一种对我仍然不起作用的方式进行操作:blog.inflinx.com/2010/03/10/…
【解决方案2】:

我终于明白了!

我不得不使用对象名称而不是 ClassName,它就像一个魅力。 NotNull.person.age=THE AGE CANNOT BE NULL!!!!

我将代码上传到 github 以供其他成员进一步参考。

【讨论】:

  • 使用 Spring Boot 1.4.1 对我来说就像这样完美地工作
【解决方案3】:

正如@Slava 所建议的,问题在于使用 JSR-303 验证器。

也许这会有所帮助:

Thymeleaf : How to use Custom Message Key in JSR-303 Annotation

【讨论】:

  • 该链接也包含在我的问题描述中。我一步步跟着它没有结果
猜你喜欢
  • 2018-05-07
  • 2018-04-16
  • 2016-11-27
  • 2015-07-29
  • 2017-08-29
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多