Springmvc using JSR-303 for verification @Valid

Source: Internet
Author: User

First, the 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;

Second, write the bean to be verified

1 @NotNull (message= "name cannot be empty")2private  String userName; 3 @Max (value=120,message= "The oldest cannot be checked")4privateint age ; 5 @Email (message= "Mailbox format error")6private String Email;

Iii. the method of verifying the school

1@RequestMapping ("/login")2      PublicString testvalid (@Valid user user, bindingresult result) {3         if(Result.haserrors ()) {4List<objecterror> errorlist =result.getallerrors ();5              for(Objecterror error:errorlist) {6 System.out.println (Error.getdefaultmessage ());7             }8         }9            Ten         return"Test"; One}

Note: Here a @valid parameter must be next to a bindingresult parameter, or spring will throw an exception if the checksum does not pass

The front desk can use the Spring tag library and can customize the processing

How to use the Spring tag library:

1 <%@ taglib Prefix="form"URI="Http://www.springframework.org/tags/form"%>   2   3 <HTML>   4 <Head>   5 <title>Reservation Form</title>   6 <style>   7 . Error{   8 Color:#ff0000;   9 Font-weight:Bold;   Ten }    One </style>    A </Head>    -    - <Body>    the     <Form:formMethod= "POST"Modelattribute= "VM">    -         <form:errorsPath="*"CssClass= "Error" />    -         <Table>    -             <TR>    +                 <TD>Name</TD>    -                 <TD><Form:inputPath= "UserName" />    +                 </TD>    A                 <TD><form:errorsPath= "UserName"CssClass= "Error" />    at                 </TD>    -             </TR>    -             <TR>    -                 <TD>Email</TD>    -                 <TD><Form:inputPath= "Email" />    -                 </TD>    in                 <TD><form:errorsPath= "Email"CssClass= "Error" />    -                 </TD>    to             </TR>    +         -             <TR>    the                 <TDcolspan= "3"><inputtype= "Submit" />    *                 </TD>    $             </TR>   Panax Notoginseng         </Table>    -     </Form:form>    the </Body>    + </HTML>

Iv. opening Spring's valid function

1 <  />
1 empty Check2 @Null verifies whether the object is Null3 @NotNull Verify that the object is not null and cannot search for a string of length 04 @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.5 @NotEmpty checks whether the constraint element is null or empty.6  7 Booelan Check8@AssertTrue Verify that the Boolean object istrue  9@AssertFalse Verify that the Boolean object isfalse  Ten   One length Check A@Size (min=, max=verifies whether 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. -   the 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 "", integer is null + @Min Verify that number and String objects are large equal to the specified value A @Max Verify that number and String objects are small equals the specified value at @DecimalMax The value that is 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 that is being labeled must be not less than the minimum value 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, and fraction specifies the decimal precision.  -   -@Range (min=, max=) Checks whether the annotated value lies between (inclusive) the specified minimum and maximum. in@Range (min=10000,max=50000,message= "Range.bean.wage") - PrivateBigDecimal wage; to   + 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 the @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=)

Springmvc using JSR-303 for verification @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.