[英]@NotNull and @size Spring MVC Validation does not work and does not display the custom message
驗證沒有完成,我找不到錯誤在哪里,即使我輸入一個空字段,它也會通過並且沒有顯示任何消息,大小也是同樣的問題。 唯一有效的驗證是“freePases”字段的最小值和最大值,它不允許我輸入超過 10 位數字,但那里也沒有顯示任何消息,我希望驗證以確保數字介於 0 和10,不像現在那樣包含 0 到 10 位數字。 我使用了 hibernate 驗證器 7 和驗證 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。
這篇文章還可以幫助您找到更具描述性的答案。 @NotNull 注釋在 Spring 啟動應用程序中不起作用
如果你沒有 pom.xml,
將 jar 文件添加到您的lib
文件夾。 但正是您新添加的 jars,您可以在外部庫文件夾中看到。 使用這些鏈接,您可以下載hibernate-validator.jar和validation-api-2.0.1.final.jar 。
這篇文章可能會幫助您將這些 jar 文件添加到 lib 文件夾將外部 jars (lib/*.jar) 添加到 IntelliJ IDEA 項目的正確方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.