【问题标题】:@NotNull is not validating and not Producing correct Result in Spring Framework@NotNull 没有在 Spring Framework 中验证并且没有产生正确的结果
【发布时间】:2019-02-11 02:38:31
【问题描述】:

我创建了一个简单的应用程序来检查给定的名字和姓氏是否为空。我已经使用@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>

【问题讨论】:

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


    【解决方案1】:

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

    @NotNull
    

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

    @NotEmpty
    

    字符串不能为空且不为空(大小> 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
    

    【讨论】:

      【解决方案2】:
      <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 中添加这个依赖解决了这个问题

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 2014-03-14
        • 2011-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-29
        • 2017-02-15
        • 2019-07-04
        相关资源
        最近更新 更多