@NotNull 和@size Spring MVC 验证不起作用并且不显示自定义消息
@NotNull and @size Spring MVC Validation does not work and does not display the custom message
没有验证,找不到错误的地方,即使我输入一个空字段,它也通过了,没有任何信息显示,大小也是一样的问题。
唯一有效的验证是“freePases”字段的最小值和最大值,它不允许我输入超过 10 位数字,但那里也没有显示任何消息,我希望验证以确保数字介于 0 和10,不像现在那样包含 0 到 10 位数字。
我使用了 hibernate validator 7 和 validation Api 2.0.1 并且我使用了 IntelliJ Idea。
我不知道该怎么办,先谢谢你的回答。
package com.spring.demo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required")
@Size(min=1, message="is required")
private String lastName;
@Min(value=0, message="must be greater than or equal to zero")
@Max(value = 10, message="must be less than or equal to 10")
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;
}
}
package com.spring.demo;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@RequestMapping("/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(
@Valid @ModelAttribute("customer") Customer theCustomer,
BindingResult theBindingResult) {
//seeing if we have white spaces and if they passes validation
System.out.println("Last name: |" + theCustomer.getLastName() + "|");
if (theBindingResult.hasErrors()) {
return "customer-form";
}
else {
return "customer-confirmation";
}
}
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Customer Registration Form</title>
<style>
.error {color:red}
</style>
</head>
<body>
<i>Fill out the form, Asterisk (*) means required</i>
<br><br>
<form:form action="processForm" modelAttribute="customer">
First name :<form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/>
<form:errors paths="lastName" cssClass="error"/>
<br><br>
Free Passes (*): <form:input path="freePasses"/>
<form:errors paths="freePasses" cssClass="error"/>
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
你必须在 pom.xml
下面添加这个
Bean 验证 API 2.0.1:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
Hibernate 验证器 7.0.1 最终版:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
然后你必须更新maven。
此 post 还可以帮助您找到更具描述性的答案。 @NotNull annotation is not working in Spring boot application
如果你没有 pom.xml,
将 jar 文件添加到您的 lib
文件夹。但正是你新添加的罐子,你可以在外部库文件夹中看到。使用这些链接,您可以下载 hibernate-validator.jar and validation-api-2.0.1.final.jar.
这 post 可以帮助您将这些 jar 文件添加到 lib 文件夹 Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project
package com.spring.demo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required")
@Size(min=1, message="is required")
private String lastName;
@Min(value=0, message="must be greater than or equal to zero")
@Max(value = 10, message="must be less than or equal to 10")
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;
}
}
package com.spring.demo;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@RequestMapping("/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(
@Valid @ModelAttribute("customer") Customer theCustomer,
BindingResult theBindingResult) {
//seeing if we have white spaces and if they passes validation
System.out.println("Last name: |" + theCustomer.getLastName() + "|");
if (theBindingResult.hasErrors()) {
return "customer-form";
}
else {
return "customer-confirmation";
}
}
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Customer Registration Form</title>
<style>
.error {color:red}
</style>
</head>
<body>
<i>Fill out the form, Asterisk (*) means required</i>
<br><br>
<form:form action="processForm" modelAttribute="customer">
First name :<form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/>
<form:errors paths="lastName" cssClass="error"/>
<br><br>
Free Passes (*): <form:input path="freePasses"/>
<form:errors paths="freePasses" cssClass="error"/>
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
你必须在 pom.xml
下面添加这个Bean 验证 API 2.0.1:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
Hibernate 验证器 7.0.1 最终版:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.1.Final</version>
</dependency>
然后你必须更新maven。
此 post 还可以帮助您找到更具描述性的答案。 @NotNull annotation is not working in Spring boot application
如果你没有 pom.xml,
将 jar 文件添加到您的 lib
文件夹。但正是你新添加的罐子,你可以在外部库文件夹中看到。使用这些链接,您可以下载 hibernate-validator.jar and validation-api-2.0.1.final.jar.
这 post 可以帮助您将这些 jar 文件添加到 lib 文件夹 Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project