Spring boot + Thymeleaf自定义错误消息

时间:2015-12-24 15:10:37

标签: spring-boot thymeleaf

我正在使用spring boot和thymeleaf进行网络应用。

我想自定义验证错误消息,而不是使用默认消息。我发现这样做的方法是创建一个名为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放在资源文件夹中:

enter image description here

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

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

3 个答案:

答案 0 :(得分:3)

因为您使用的是JSR-303验证器,所以您应该将消息发送到名为<div class="body"> <div class="top-border"></div> <div class="section1"> <div class="what-i-do"> <img class="what-i-do-icon" src="images/what-i-do.png" /> <h1 class="what-i-do-title">What I Do</h1> <p class="what-i-do-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu, a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p> <p class="view-more-btn">View More</p> </div> <div class="development"> <img class="development-icon" src="images/development.png" /> <h1 class="development-title">Development</h1> <p class="development-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu, a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p> </div> <div class="design"> <img class="design-icon" src="images/design.png" /> <h1 class="design-title">Design</h1> <p class="design-desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam semper quam arcu, a consequat tellus cursus vel. Vivamus lacus massa, feugiat non malesuada sed, efficitur eu elit. </p> </div> </div> </div>的文件。 JSR-303提供程序(在本例中为hibernate-validator)默认查找此文件(根据规范)。

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

答案 1 :(得分:2)

最后我明白了!

我不得不使用ClassName,而是使用对象名称,它就像魅力一样。 https://jsfiddle.net/ayd4x079/2/

我将代码上传到github以获取任何其他成员的进一步参考。

答案 2 :(得分:0)

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

也许这会有所帮助:

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

相关问题