使用SpringBoot中的validate.properties文件验证表单

时间:2017-01-29 21:03:03

标签: java jsp spring-boot

我正在尝试在Spring Boot中验证表单,当我点击提交按钮时,jsp会向我显示消息如何填充字段(或不是)。我创建了validation.properties,我有消息,但我总是得到错误"No message found under code for locale 'en_US'"
有人知道如何在jsp中显示这些消息吗?我的validation.properties文件位于src/main/resources/static文件夹中,在我的jsp文件中,我放置了validation.properties文件<link rel="stylesheet" href="${pageContext.request.contextPath}/validation.properties"></link>的链接。 在验证课上我有:

public void validate(Object target, Errors errors) {
    UserRegistration userRegistrated = (UserRegistration) target;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "ime", "NotEmpty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "prezime", "NotEmpty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "NotEmpty");

    if (service.findUsers(userRegistrated.getEmail())) {
        errors.rejectValue("email", "ExistUser");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (userRegistrated.getPassword().length() < 4) {
        errors.rejectValue("password", "LengthPass");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "repeatPassword", "NotEmpty");
    if (!userRegistrated.getRepeatPassword().equals(userRegistrated.getPassword())) {
        errors.rejectValue("repeatPassword", "MatchPass");
    }
}

其中imeprezimeemailpasswordrepeatPassword对应于bean UserRegistration中的字段:

class UserRegistration {
    private String ime;
    private String prezime;
    private String email;
    private String password;
    private String repeatPassword;

    public UserRegistration() {
    }

    public String getIme() {
        return ime;
    }

    public void setIme(String ime) {
        this.ime = ime;
    }

    public String getPrezime() {
        return prezime;
    }

    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRepeatPassword() {
        return repeatPassword;
    }

    public void setRepeatPassword(String repeatPassword) {
        this.repeatPassword = repeatPassword;
    }

    @Override
    public String toString() {
        return "UserRegistration [ime=" + ime + ", prezime=" + prezime + ", email=" + email + ", password=" + password
                + ", repeatPassword=" + repeatPassword + "]";
    }
}

我在jsp中的表单如下:

<form:form id="register" action="${action}" method="post" modelAttribute="userRegistration">
    <table>
        <tr>
            <td><form:label path="ime">Ime:</form:label></td>
            <td><form:input path="ime" type="text" placeholder="Ime" /></td>
            <td><form:errors path="ime" /></td>
        </tr>
        <tr>
            <td><form:label path="prezime">Prezime:</form:label></td>
            <td><form:input path="prezime" type="text" placeholder="Prezime"/></td>
            <td><form:errors path="prezime" /></td>
        </tr>
        <tr>
            <td><form:label path="email">Korisnicko ime(e-mail):</form:label></td>
            <td><form:input path="email" type="text" placeholder="Email" /></td>
            <td><form:errors path="email" /></td>
        </tr>
        <tr>
            <td><form:label path="password">Lozinka:</form:label></td>
            <td><form:input path="password" type="password" placeholder="Lozinka"/></td>
            <td><form:errors path="password" /></td>
        </tr>
        <tr>
            <td><form:label path="repeatPassword">Potvrda lozinke:</form:label></td>
            <td><form:input path="repeatPassword" type="password" placeholder="Potvrda lozinke"/></td>
            <td><form:errors path="repeatPassword" /></td>
        </tr>
        <tr><td colspan="3"><button type="submit">Submit</button></td></tr>
    </table>
</form:form>

我的validation.properties文件:

NotEmpty.UserRegistration.ime=Polje je obavezno!
NotEmpty.UserRegistration.prezime=Polje je obavezno!
NotEmpty.UserRegistration.email=Polje je obavezno!
NotEmpty.UserRegistration.password=Polje je obavezno!
NotEmpty.UserRegistration.repeatPassword=Polje je obavezno!
ExistUser.UserRegistration.email=Korisnik sa takvim e-mailom vec postoji u bazi!
LengthPass.UserRegistration.password=Lozinka treba da je duza od 4 karaktera!
MatchPass.UserRegistration.repeatPassword=Lozinke se ne poklapaju!

我曾尝试将validation.properties放在src/main/javasrc/main/resources个文件夹中,但我得到了相同的结果。我认为问题是如何访问该validation.properties文件,但我没有&# 39;知道怎么做吗?我需要为它编写一些配置吗?我搜索了互联网,主要是针对春天的解释(不是春季启动),我不知道它是不是同样......提前谢谢!

0 个答案:

没有答案