There is a basic method of data validation in spring 4.0 annotation data validation 1. or the Pojo:
PackageCom.suyin.pojo;ImportJava.lang.reflect.Field;ImportOrg.hibernate.validator.constraints.NotBlank; Public classpeople {Private intID; @NotBlankPrivateString name; PrivateString 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
There are requirements to ensure that name is not empty when adding, and that name and address are not empty when modifying. What about this?
SPRING4.0 provides a solution. Write two first and second, similar to Java.io.Serializable, as follows:
Package Com.suyin.my.va; Public Interface First {}
And
Package Com.suyin.my.va; Public Interface Second {}
The retrofit people is as follows:
PackageCom.suyin.pojo;ImportJava.lang.reflect.Field;ImportOrg.hibernate.validator.constraints.Email;ImportOrg.hibernate.validator.constraints.NotBlank;ImportCom.suyin.my.va.First;ImportCom.suyin.my.va.Second; Public classpeople {Private intID; @NotBlank (Groups= {first.class, Second.class}) PrivateString name; @NotBlank (Groups= {Second.class}) PrivateString 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
Added a @NotBlank (groups= {first) to name. Class, Second. class})
Added a @NotBlank (groups= {Second) to address. Class})
You can guess that when you encounter the first flag, name is verified and name and address are verified when you encounter the second flag. Now just tell spring to use the first or the second flag for validation.
There are methods in the controller
@RequestMapping (value= "/add") PublicString Add (@Validated first.class) People p,bindingresult result, httpservletrequest request) {if(Result.haserrors ()) {System.out.println (result.getfielderrors ()); return"Error"; } This. Peopleservice.add (P); return"People/detail"; } @RequestMapping ("/update") PublicString Update (@Validated (Second.class) People p,bindingresult result,httpservletrequest request) {if(Result.haserrors ()) {System.out.println (result.getfielderrors ()); return"Error"; } This. Peopleservice.update (P); return"People/detail"; }
View Code
Before each method parameter people p, the original @valid was changed to @validated, and the parameters First.class and Second.class were added respectively.
Spring 4.0 annotation Data validation 2