Spring--《Spring实战》第6章ValidationMessages.properties使用问题

package spittr.model;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Created by hg_yi on 17-11-7.
 */
public class Spitter {
    private Integer id;

    @NotNull
    @Size(min=5, max=16, message="{username.size}")
    private String username;

    @NotNull
    @Size(min=5, max=25, message="{password.size}")
    private String password;

    @NotNull
    @Size(min=2, max=30, message="{firstName.size}")
    private String firstName;

    @NotNull
    @Size(min=2, max=30, message="{lastName.size}")
    private String lastName;
}

如上面代码,想要在NotSize中使用message(注解校验同样需要),我们需要做两件事情:

1.给pom.xml中添加依赖:


        <dependency>
            <groupId>javax.validationgroupId>
            <artifactId>validation-apiartifactId>
            <version>1.1.0.Finalversion>
        dependency>

        <dependency>
            <groupId>javax.elgroupId>
            <artifactId>el-apiartifactId>
            <version>2.2version>
        dependency>

        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-validatorartifactId>
            <version>5.1.0.Finalversion>
        dependency>

2.在根路径下的resources文件夹里面添加ValidationMessages.properties(文件名不可更改)并填写相应内容。

你可能感兴趣的:(Spring)