简体   繁体   English

@NotNull 和@size Spring 验证不起作用

[英]@NotNull and @size Spring Validation doesn't work

Problem was solved: The problem is Hibernate Validator 7, which is a JakartaEE implementation and not a JavaEE implementation of the validation API. Use Hibernate Validator 6 which is compatible with Spring 5. For JakartaEE you need Spring 6 which is still in development at this moment.问题已解决:问题是 Hibernate Validator 7,它是 JakartaEE 实现,而不是验证 API 的 JavaEE 实现。使用 Hibernate Validator 6,它与 Spring 5 兼容。对于 JakartaEE,您需要 Spring 仍在开发中 6片刻。 – M. Deinum – M. Deinum

So am doing spring and hibernate course and got to @NotNull and @size chapter.所以我正在学习 spring 和 hibernate 课程并学习了@NotNull 和@size 章节。 I implemented hibernate jar files 1,2,3 4,5,6,7 and I don't get any errors in code, but @NotNull and @Size does not work, There is another post about this problem and I saw no solution there so I had to make new post.我实现了 hibernate jar files 1,2,3 4,5,6,7我没有在代码中遇到任何错误,但是 @NotNull 和 @Size 不起作用,还有另一篇关于这个问题的帖子,我没有找到解决方案在那里,所以我不得不发表新文章。

Hibernate validator I implement is hibernate-validator-7.0.3.Final and spring am using is spring-framework-5.3.9我实现的 Hibernate 验证器是 hibernate-validator-7.0.3.Final 和 spring 我使用的是 spring-framework-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";
        }
    }
    
}

try to use @NotNull @NotBlank @Size in this sequence尝试在此序列中使用@NotNull @NotBlank @Size

If you want spaces as valid string then use @NotEmpty如果您希望空格作为有效字符串,请使用 @NotEmpty

I had similar issue.我有类似的问题。

Replacing:更换:

@jakarta.validation.constraints.NotNull

with

@org.hibernate.validator.constraints.NotBlank

and

@jakarta.validation.constraints.Size

with

@org.hibernate.validator.constraints.Length

helped me帮助过我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM