The first thing to say is what is called validator:
Validator is a validator that validates data received in the background and verifies the data.
SPRINGMVC Server Authentication is available in two ways, one based on the validator interface and one using the Annotaion JSR-303 standard.
1. Validation using the Annotaion JSR-303 standard
Using this need to import a package that supports the JSR-303 standard, it is recommended to use Hibernate validator This package to see the standard native annotations first:
Limit |
Description |
@Null |
Limit is null only |
@NotNull |
Limit must be non-null |
@AssertFalse |
The limit must be false |
@AssertTrue |
The limit must be true |
@DecimalMax (value) |
Limit must be a number that is less than the specified value |
@DecimalMin (value) |
The limit must be a number that is not less than the specified value |
@Digits (integer,fraction) |
The limit must be a decimal, and the number of digits in the integer part cannot exceed integer, and the number of fractional digits cannot exceed fraction |
@Future |
The limit must be a future date |
@Max (value) |
Limit must be a number that is less than the specified value |
@Min (value) |
The limit must be a number that is not less than the specified value |
@Past |
The limit must be a past date |
@Pattern (value) |
The restriction must conform to the specified regular expression |
@Size (Max,min) |
Limit character length must be between min to Max |
@Past |
Verify that the annotation's element value (date type) is earlier than the current time |
@NotEmpty |
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) |
@NotBlank |
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 |
@Email |
Verify that the element value of the annotation is email, or you can specify a custom email cell with regular expressions and flag |
To use it very simply, precede the variable that needs to be validated with the annotation, see the user after use
1 public class user { 2 @NotEmpty (message = "User name cannot be empty" ) 3 private String username; 4 @Size (min=6, max=, message = "Password length does not conform to standard" 5 private String password; 6 private String nickname; 7 8 ... 9 }
Then the controller inside the method on the need to verify the object or data to join the @validated can be.
2. Using the Validator interface
The main thing is to do validator entity class, to implement validator interface:
Studentvalidator.java
Public classStudentvalidatorImplementsvalidator{ Public BooleanSupports (class<?>clazz) { returnStudent.class. Equals (Clazz); } Public voidValidate (Object obj, Errors e) {validationutils.rejectifempty (E,"Name", "Name.empty"); Validationutils.rejectifempty (E,"Gender", "null"); Validationutils.rejectifempty (E,"Age", "null"); Student s=(Student) obj; if(S.getage () < 18) {E.rejectvalue ("Age" and "young"); }Else if(S.getage () > 50){ //e.reject ("Age", "old"); } } }
Teachervalidator.java
Public classTeachervalidatorImplementsvalidator{ Public BooleanSupports (class<?>clazz) { returnTeacher.class. Equals (Clazz); } Public voidValidate (Object obj, Errors e) {validationutils.rejectifempty (E,"Name", "Name.empty"); Validationutils.rejectifempty (E,"Gender", "null"); Validationutils.rejectifempty (E,"Age", "null"); Teacher T=(Teacher) obj; } }
Peoplecontorller.java
@Controller Public classUsercontroller {@AutowiredPrivateStudentvalidator Studentvalidator; @AutowiredPrivateTeachervalidator Teachervalidator; @RequestMapping (Value= "/savestudent", method =requestmethod.post) Public voidsavestudent (@Valid @RequestBody Student Student) {...} @RequestMapping (Value= "/saveteacher", method =requestmethod.post) Public voidsavestudent (@Valid @RequestBody Teacher Teacher) {...} @InitBinder Public voidInitbinder (Webdatabinder binder) {//This method loads the validator, determines the object to be validated by the request, and loads the relative validator. This method is loaded according to the request, that is, n times the request is loaded n times the method if(Studentvalidator.supports (Binder.gettarget (). GetClass ())&&!studentvalidator.getclass (). GetName (). Contains ("Org.springframework") {binder.addvalidators (studentvalidator); }Else{binder.addvalidators (teachervalidator); } }}
PS: You need to make a judgment to realize the existence of multiple validators.
A method for loading multiple validator in spring MVC.