Reputation: 716
I have created a simple Application that checks whether the given first name and lastname is null or not . i have used @NotNull annotation to check
The code for the Student.java is
@NotNull(message = "Name must not be null")
private String firstName;
@NotNull(message = "Name must not be null")
private String lastName;
i have also created 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";
}
}
My HTML Form is
<br/>
<form:input path="firstName"/>
<form:input path="lastName"/>
<br/>
i havent given value for firstName and the lastName , and i have submitted the form. The form redirects to "success" page which is incorrect.
My pom.xml file is
<?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>
Upvotes: 1
Views: 1268
Reputation: 1921
When you submit the form, String
fields are posted as empty but not null. Here are some more constraints you can use:
@NotNull
String cannot be null, however can be empty.
@NotEmpty
String cannot be null and not empty (size > 0).
@NotBlank
String cannot be null and must contain at least one non-whitespace character.
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
Upvotes: 1
Reputation: 716
<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>
adding this dependency in pom.xml resolved this issue
Upvotes: 1