问题解决了:问题是Hibernate Validator 7,它是一个JakartaEE实现,而不是验证API的JavaEE实现。使用Hibernate Validator 6,它与Spring 5兼容。对于JakartaEE,您需要目前仍在开发中的Spring6。- M.德尼姆
因此,我正在做春季和冬眠课程,并进入了“NotNull”和“size”章节。我实现了hibernate jar文件1,2,3 4,5,6,7,代码中没有任何错误,但是@NotNull和@Size不起作用,还有关于这个问题的另一篇文章,我在那里没有看到解决方案,所以我不得不做新的帖子。
我实现的Hibernate验证器是hibernate-验证器-7.0.3,using和spring使用的是is框架-5.3.9
package com.luv2code.springdemo.mvc;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public class Customer {
private String firstName;
@Size(min = 1, message ="is required")
@NotNull( message ="is required")
private String lastName;
public Customer() {
}
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;
}
}/
<%@ taglib prefix= "form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title> Customer registration form</title>
<style>
.error{color:red}
</style>
</head>
<body>
<form:form action ="processForm" modelAttribute = "customer">
First name: <form:input path="firstName" />
<br><br>
Last name: <form:input path="lastName"/>
<form:errors path= "lastName" cssClass = "error"/>
<br><br>
<input type= "submit" value ="Submit" />
</form:form>
</body>
</html>/
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@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) {
if(theBindingResult.hasErrors()) {
return "customer-form";
} else {
return "customer-confirmation";
}
}
}发布于 2022-03-04 21:11:47
尝试在此序列中使用“NotNull”NotBlank“大小”
如果您希望空格作为有效字符串,那么使用@NotEmpty
发布于 2022-03-06 14:09:19
我也有过类似的问题。
取代:
@jakarta.validation.constraints.NotNull使用
@org.hibernate.validator.constraints.NotBlank和
@jakarta.validation.constraints.Size使用
@org.hibernate.validator.constraints.Length帮我
https://stackoverflow.com/questions/71354967
复制相似问题