Typically, data validation is divided into foreground validation and background validation. And the front desk JS verification is certain, then actually verify the error message does not have to pass through the background, even if it is to internationalization, the front desk JS can also be competent.
If the front-end verification is sufficient, then if there are incorrect information to the backstage, it is most likely through illegal means. Then I do not have to be polite to this information, the direct denial of the line, even the error message does not have to send. Based on this idea,
Background verification saves a lot of things.
We now use hibernate for that set of validations and must introduce the Hibernate-validator jar package. Go to Maven Repository to find it. If you can not find it, get Baidu Cloud to find it
Existing Pojo classes:
ImportJava.lang.reflect.Field;ImportOrg.hibernate.validator.constraints.Email;ImportOrg.hibernate.validator.constraints.NotEmpty; Public classpeople {Private intID; @NotEmptyPrivateString name; @EmailPrivateString address; Private intAge ; Public Final intgetId () {returnID; } Public Final voidSetId (intID) { This. ID =ID; } Public FinalString GetName () {returnname; } Public Final voidsetName (String name) { This. Name =name; } Public FinalString getaddress () {returnaddress; } Public Final voidsetaddress (String address) { This. Address =address; } Public Final intGetage () {returnAge ; } Public Final voidSetage (intAge ) { This. Age =Age ; } @Override PublicString toString () {return"People [id=" + ID + ", name=" + name + ", address=" +Address+ ", age=" + Age + "]"; } Publicpeople set (String name, Object obj) {Try{Field F= This. GetClass (). Getdeclaredfield (name); F.setaccessible (true); F.set ( This, obj); } Catch(Nosuchfieldexception |SecurityException| IllegalArgumentException |illegalaccessexception e) {E.printstacktrace (); } return This; }}View Code
You can see that there is a @notempty on the property name and there is a @email on the address of the attribute (which is, of course, pretended).
There are methods in the controller:
@RequestMapping (value= "/add", method=requestmethod.post) public String Add (@Valid people P,bindingresult result, httpservletrequest request) { if(Result.haserrors ()) return "error"; This . Peopleservice.add (p); return "People/detail"; }
View Code
Note that there is an annotation @valid before the parameter p, and immediately after P is bindingresult result. Bindingresult result must be followed by people P.
If there is an error, return "error" via the code if(Result.haserrors ()); directly to the error interface, the error interface does not even exist ...
In addition, this has the advantage that the type of age in people is int. What if the pass argument is age is a string that cannot be converted to int?
Haha, the return value of result.haserrors () is also true, that is, the error interface is returned.
Add one point: Non-null validation is the most used, and you will see @NotEmpty, @NotNull, @NotBlank three similar annotations
[email protected] for any reference type, verify that it is not null
[Email protected]. Used CharSequence , Collection , Map and Arrays , validation is not null CharSequence and length is not 0,collection,map,array size is not 0
[Email protected]. Used CharSequence for. Validation is not NULL, if not NULL, then verify length after trim is not 0
All daily validation strings, use @NotBlank .
For annotations that can be verified directly on-line copy.
Annotations |
Applicable data types |
Description |
@AssertFalse |
Boolean, Boolean |
Verify that the element value of the annotation is false |
@AssertTrue |
Boolean, Boolean |
Verify that the element value of the annotation is true |
@DecimalMax (value=x) |
BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive. Additionally supported by Hv:any sub-type of number andcharsequence. |
Verifies that the element value of the annotation is less than or equal to the value specified by @ Decimalmax |
@DecimalMin (value=x) |
BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive. Additionally supported by Hv:any sub-type of number andcharsequence. |
Verifies that the element value of the annotation is less than or equal to the value specified by @ decimalmin |
@Digits (integer= integer digits, fraction= decimal place) |
BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive. Additionally supported by Hv:any sub-type of number andcharsequence. |
To verify the integer and maximum number of decimal digits for the element value of the annotation |
@Future |
Java.util.Date, Java.util.Calendar; Additionally supported by HV, if Thejoda time Date/time API was on the class Path:any implementations ofreadablepartial an Dreadableinstant. |
Verify that the annotation's element value (date type) is later than the current time |
@Max (value=x) |
BigDecimal, BigInteger, Byte, Short,int, long and the respective wrappers of the primitive types. Additionally supported by Hv:any sub-type ofcharsequence (the numeric value represented by the character sequence is eval uated), any sub-type of number. |
Verifies that the element value of the annotation is less than or equal to the value specified by @max |
@Min (value=x) |
BigDecimal, BigInteger, Byte, Short,int, long and the respective wrappers of the primitive types. Additionally supported by Hv:any sub-type of charsequence (the numeric value represented by the char sequence is evaluate d), any sub-type of number. |
Verifies that the element value of the annotation is greater than or equal to the value specified by @min |
@NotNull |
Any type |
Verify that the element value of the annotation is not null |
@Null |
Any type |
Verify that the element value of the annotation is null |
@Past |
Java.util.Date, Java.util.Calendar; Additionally supported by HV, if Thejoda time Date/time API was on the class Path:any implementations ofreadablepartial an Dreadableinstant. |
Verify that the annotation's element value (date type) is earlier than the current time |
@Pattern (regex= Regular expression, flag=) |
String. Additionally supported by Hv:any sub-type of Charsequence. |
Validates that the element value of the annotation matches the specified regular expression |
@Size (min= min, max= max) |
String, Collection, Map and arrays. Additionally supported by Hv:any sub-type of Charsequence. |
Verifies that the element value of the annotation is within a specified interval of min and max (inclusive), such as character length, collection size |
@Valid |
Any non-primitive type (reference type) |
Validates the associated object, such as an order object in the account object, specifying the validation order object |
@NotEmpty |
CharSequence,Collection,Map and Arrays
|
Verifies that the element value of the annotation is not null and is not empty (string length is not 0, collection size is not 0) |
@Range (min= min, max= max) |
CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types
|
Verifies that the element value of the annotation is between the minimum and maximum values |
@NotBlank |
CharSequence
|
Verifies that the element value of the annotation is not null (NOT NULL, the first space is removed after the length is 0), differs from @notempty, @NotBlank is applied only to strings and the whitespace is stripped of the string when compared |
@Length (min= lower limit, max= upper limit) |
CharSequence
|
Validates the element value length of annotations within min and Max intervals |
@Email |
CharSequence
|
Verify that the element value of the annotation is email, or you can specify a custom email format via regular expressions and flag |
For more information, please refer to the official documentation: http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-usingvalidator.html
Spring 4.0 annotation Data validation 1