Use JSR-303 to verify @Valid

Source: Internet
Author: User

First, use in the SRINGMVC

Using annotations

1. Jar to be used when preparing the calibration

VALIDATION-API-1.0.0.GA.JAR:JDK interface;

Hibernate-validator-4.2.0.final.jar is the realization of the above interface;

log4j, SLF4J, slf4j-log4j

2. Write the bean to be verified

@NotNull (message= "name cannot be empty")private String userName; @Max (value=120,message= "oldest")int Age ; @Email (message= "Mailbox format error")private String Email;       

Empty check
@Null verifies whether the object is Null
@NotNull Verify that the object is not null and cannot search for a string of length 0
@NotBlank checks if the constraint string is null and if the length of trim is greater than 0, only the string, and the front and back spaces are removed.
@NotEmpty checks whether the constraint element is null or empty.

Booelan Check
@AssertTrue verifies whether a Boolean object is True
@AssertFalse Verify that the Boolean object is False

Length Check
@Size (min=, max=) verifies that the length of the object (array,collection,map,string) is within a given range
@Length (min=, max=) validates, the annotated string is between Min and Max included.

Date Check
@Past Verify that date and Calendar objects are before the current time
@Future Verify that date and Calendar objects are after the current time
@Pattern to verify that a String object conforms to a regular expression rule

Numeric check, recommended for use in Stirng,integer type, is not recommended for type int, because the form value is "" cannot be converted to int, but can be converted to stirng to "" and integer is null
@Min Verify that number and String objects are large equal to the specified value
@Max Verify that number and String objects are small equals the specified value
@DecimalMax the value that is being labeled must not be greater than the maximum value specified in the constraint. The parameter of this constraint is a string representation of the maximum value defined by BigDecimal. Decimal Presence Precision
@DecimalMin the value to be labeled must be not less than the minimum specified in the constraint. The parameter of this constraint is a string representation of the minimum value defined by BigDecimal. Decimal Presence Precision
@Digits Verify that the composition of number and String is legal
@Digits (integer=,fraction=) verifies that the string is a number that conforms to the specified format, interger Specifies the integer precision, fraction specifies the decimal precision.

The @Range (min=, max=) checks whether the number is between Min and Max.
@Range (min=10000,max=50000,message= "Range.bean.wage")
Private BigDecimal wage;

The @Valid recursively verifies the associated object, and if the associated object is a collection or an array, the element is recursively checked, and if it is a map, the value part of it is verified. (whether recursive validation is performed)
@CreditCardNumber Credit Card Verification
@Email Verify that the e-mail address, if NULL, is not validated and is validated.
@ScriptAssert (lang=, script=, alias=)
@URL (protocol=,host=, port=,regexp=, flags=)

Use JSR-303 to verify @Valid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.