为什么spring验证注解不起作用?

brccelvz  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(261)

嗨,我坚持了几个星期,我找了很多,但没有找到答案,所以请帮助我,如果你可以:
这是我的客户类:

public class Customer {
private String firstName;
@Valid

@NotNull(message="")

private String lastName;

@Min(value=0 , message="bigger than zero")
@Max(value=10 , message="less than ten")
private int freePasses;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

public int getFreePasses() {
    return freePasses;
}

public void setFreePasses(int freePasses) {
    this.freePasses = freePasses;
}

这是我的控制器:

public class CustomerController {

@RequestMapping("/showForm")
public String showForm(Model model) {
     Customer thecustomer=new Customer();
     model.addAttribute("customer",thecustomer);
     //first one is name and the second one is value 
     return "customer-form";
}

@RequestMapping("/processForm")
 public String processForm(@Valid @ModelAttribute("customer")  Customer thecustomer,BindingResult theresult,Errors error)
{
    System.out.println(thecustomer.getLastName());
    if(theresult.hasErrors()) {
        System.out.println("!!!");
        return "customer-form";}
    else {
        return "customer-confirmation";}

}

此条件if(theresult.haserrors())在max、min和notnull未验证的条件下总是返回true:(为什么???
这是我的html前端,以备您检查:

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"  %>
<!DOCTYPE html>
<html>
<head>
<title>customer registration</title>

<style > 

.error{color:red}

</style>

</head>

<body>

<form:form action="processForm" modelAttribute="customer">
<p>firstname:</p> <form:input path="firstName"/>
<br>
<p>lastname:</p> <form:input path="lastName"  />

<form:errors path="lastName" cssClass="error"/>
<br>

 <form:input path="freePasses"  />

<form:errors path="freePasses" cssClass="error"/>

<input type="submit" value="Submit">

</form:form>

</body>

</html>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题