【问题标题】:@NotNull and @size Spring MVC Validation does not work and does not display the custom message@NotNull 和 @size Spring MVC 验证不起作用并且不显示自定义消息
【发布时间】:2021-06-29 06:50:36
【问题描述】:

验证没有完成,我找不到错误在哪里,即使我输入一个空字段,它也通过并且没有任何消息显示,大小也是同样的问题。 唯一有效的验证是“freePases”字段的最小值和最大值,它不允许我输入超过 10 位数字,但那里也没有显示任何消息,我希望验证确保数字介于 0 和10,不像现在那样包含 0 到 10 位数字。 我使用了休眠验证器 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>

【问题讨论】:

    标签: spring hibernate validation spring-mvc intellij-idea


    【解决方案1】:

    你必须在 pom.xml 的下面添加这个

    Bean 验证 API 2.0.1:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>
    

    休眠验证器 7.0.1 最终版:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>7.0.1.Final</version>
     </dependency>
    

    那么你必须更新maven。

    这篇文章还可以帮助您找到更具描述性的答案。 @NotNull annotation is not working in Spring boot application

    如果你没有 pom.xml,

    将 jar 文件添加到您的 lib 文件夹。但正是您新添加的罐子,您可以在外部库文件夹中看到。使用这些链接,您可以下载hibernate-validator.jarvalidation-api-2.0.1.final.jar

    这篇文章可能会帮助您将这些jar文件添加到lib文件夹Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

    【讨论】:

    • 我没有 pom.xml 文件,我刚刚在 Intellij 中创建了一个 SpringMVC 项目,我应该在 applicationContext.xml 中添加一些东西吗?
    • 你能添加一张你的文件夹结构的图片吗?
    • 你用过maven吗?
    • 我添加了我的项目图片
    猜你喜欢
    • 2019-02-22
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多