Spring MVC休眠验证不起作用

时间:2019-07-26 14:46:18

标签: java spring validation spring-mvc hibernate-validator

我的姓和名字段没有任何验证错误。我尝试将休眠依赖项添加到Maven,但仍然无法正常工作。它只会转到下一页而不会出现任何错误。如何解决这个问题?

客户类别:

public class Customer {

    @NotNull(message = "is required")
    @Size(min = 3, max = 20, message = "is required")
    private String firstName;

    @NotNull(message = "is required")
    @Size(min = 3, max = 20, message = "is required")
    private String lastName;
...
}

控制器:

 @RequestMapping("/processForm")
    public String processForm(@ModelAttribute("customer") @Valid Customer customer, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "customer-form";
        }
        System.out.printf("First name: %s\n Last name: %s\n", 
        customer.getFirstName(), customer.getLastName());
        return "customer-confirmation";
    }

表格:

<form:form action="processForm" modelAttribute="customer" method="post">
    <p>First name (*): <form:input path="firstName"/></p>
    <form:errors path="firstName" cssClass="error"/>
    <p>Second name: <form:input path="lastName"/></p>
    <form:errors path="lastName" cssClass="error"/>
    <input type="submit" value="Submit"/>
</form:form>

行家:

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>

0 个答案:

没有答案