@NotNull 未在 Spring 框架中验证且未生成正确的结果

@NotNull is not validating and not Producing correct Result in Spring Framework

我创建了一个简单的应用程序来检查给定的名字和姓氏是否为空。我已经使用@NotNull 注释来检查

Student.java 的代码是

    @NotNull(message = "Name must not be null")
    private String firstName;
    @NotNull(message = "Name must not be null")
    private String lastName;

我也创造了StudentController.java

 public String processForm(@Valid @ModelAttribute("student") Student student,BindingResult bindingResult)
{
    //bindingResult is not an annotation
    if(bindingResult.hasErrors())
    {
        return "error";
    }
    else
    {
        return "success";
    }
}

我的HTML表格是

<br/>
<form:input path="firstName"/>
<form:input path="lastName"/>
<br/>

我还没有为 firstName 和 lastName 赋值,我已经提交了表格。表单重定向到不正确的 "success" 页面。

我的pom.xml文件是

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>org.sample.mvc</groupId>
<artifactId>formspringmvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>

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

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

当您提交表单时,String 字段被发布为 empty 而不是 null。以下是您可以使用的更多限制条件:

@NotNull

字符串不能为空,但可以为空。

@NotEmpty

字符串不能为 null 且不为空(大小 > 0)。

@NotBlank

字符串不能为空且必须至少包含一个非空白字符。

String firstName = null;

// @NotNull: false
// @NotEmpty: false
// @NotBlank: false

String firstName = "";

// @NotNull: true
// @NotEmpty: false
// @NotBlank: false

String firstName = " ";

// @NotNull: true
// @NotEmpty: true
// @NotBlank: false

String firstName = "er-han";

// @NotNull: true
// @NotEmpty: true
// @NotBlank: true
<dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.9.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.17.Final</version>
        </dependency>

在 pom.xml 中添加此依赖项解决了此问题