Spring MVC data validation

Spring MVC data validation

 

Spring3 began to support the JSR-303 validation framework, JSR-303 supports XML-style and annotation-style validation.

 

 

example

package cn.javass.chapter7.model;  
import javax.validation.constraints.NotNull;  
public class UserModel {  
    @NotNull(message="{username.not.empty}")  
    private String username;  
}   

 

 

 

The built-in validation constraint annotations are shown in the following table (taken from the hibernate validator reference):

Validate annotations

Validated data type

illustrate

@AssertFalse

Boolean,boolean

Validate that the element value of the annotation is false

@AssertTrue

Boolean,boolean

Verify that the element value of the annotation is true

@NotNull

any type

Verify that the element value of the annotation is not null

@Null

any type

Validate that the element value of the annotation is null

@Min(value=value)

BigDecimal,BigInteger, byte,

short, int, long, etc. any Subtype of Number or CharSequence (which stores numbers)

Verify that the element value of the annotation is greater than or equal to the value specified by @Min

@Max(value=value)

Same as @Min asked

Verify that the element value of the annotation is less than or equal to the value specified by @Max

@DecimalMin(value=值)

Same as @Min asked

Verify that the element value of the annotation is greater than or equal to the value specified by @DecimalMin

@DecimalMax(value=值)

Same as @Min asked

Verify that the element value of the annotation is less than or equal to the value specified by @DecimalMax

@Digits(integer=integer digits, fraction=decimal digits)

Same as @Min asked

Validate the maximum number of integers and decimals for the element value of the annotation

@Size(min=lower limit, max=upper limit)

String, Collection, Map, Array, etc.

Verify that the element value of the annotation is within the specified interval of min and max (inclusive), such as character length, set size

@Past

java.util.Date,

java.util.Calendar;

Date type of the Joda Time library

Validate that the element value of the annotation (date type) is earlier than the current time

@Future

Same as @Past request

Verify that the element value of the annotation (date type) is later than the current time

@NotBlank

CharSequence subtype

Verify that the element value of the annotation is not empty (not null, the length is 0 after removing the first space), unlike @NotEmpty, @NotBlank only applies to strings and will remove the first space of the string when comparing

@Length(min=lower limit, max=upper limit)

CharSequence subtype

Verify that the length of the element value of the annotation is within the min and max intervals

@NotEmpty

CharSequence subtype, Collection, Map, Array

Verify that the element value of the annotation is not null and not empty (string length is not 0, collection size is not 0)

@Range(min=minimum, max=maximum)

Atomic types and wrapper types such as BigDecimal, BigInteger, CharSequence, byte, short, int, long

Verify that the element value of the annotation is between the minimum and maximum values

@Email(regexp=regular expression,

flag=flag mode)

CharSequence subtype (eg String)

Verify that the element value of the annotation is Email, or you can specify a custom email format through regexp and flag

@Pattern(regexp=regular expression,

flag=flag mode)

String, any subtype of CharSequence

Verify that the element value of the annotation matches the specified regular expression

@Valid

any non-atomic type

Specify the object to recursively verify the association;

If there is an address object property in the user object, if you want to verify the address object together with the user object, add the @Valid annotation to the address object to cascade verification

 

Only most of the validation constraint annotations provided by Hibernate Validator are listed here. Please refer to the official documentation of hibernate validator for other validation constraint annotations and custom validation constraint annotation definitions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326495523&siteId=291194637