Spring 4.0 annotation Data validation 2

Source: Internet
Author: User

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

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.