@NotNull未在Spring Framework中验证并且未产生正确的结果

时间:2018-09-05 21:46:38

标签: spring spring-mvc spring-boot spring-data spring-mvc-test

我创建了一个简单的应用程序,用于检查给定的名字和姓氏是否为null。我已经使用@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赋值,并且我已经提交了表格。表单重定向到不正确的“成功”页面。

我的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>

2 个答案:

答案 0 :(得分:1)

提交表单时,String字段发布为,但不发布为。您可以使用以下一些限制条件:

@NotNull

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

@NotEmpty

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

@NotBlank

字符串不能为null,并且必须至少包含一个非空格字符。

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

答案 1 :(得分:1)

<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中添加此依赖项解决了此问题