java - 为什么弹簧验证注释不起作用?
问题描述
嗨,我坚持了几个星期,我搜索了很多,但没有找到答案,所以如果可以的话,请帮助我:
这是我的客户类:
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>
解决方案
推荐阅读
- rx-java - 阻止进度,直到所有 Observable 的值都发出
- kubernetes - 为什么我的 GKE 节点池不会自动缩减?
- swiftui - 在 SwiftUI 中将图像与 navigationBarTitle 对齐
- oracle - 在 reponsys 中连接作业通知警报
- c - 无法访问参数
- java - 使用 IntelliJ 连接到 mysql 数据库
- html - 创建了一个带有 3 种不同颜色边框的 CSS 三角形,但是可以用更少/更简单的 CSS 代码来完成吗?
- java - 使用 Mongo Reactive Repositories 配置 HikariCP
- python - 将 Mathematica 优化问题转换为 Python
- decimal - Msaccess cratch:与十进制字段相关的 VBA 错误