Springmvc using Hibernate validator to do field validation

Source: Internet
Author: User

1. Add Hiberbate Validator Related jar Package

2. The words need to be validated on the Formbean to add validation annotations, built-in annotations are:

    1. Dbean built-in constraint in Validation
    2. @Null The annotated element must be Null
    3. @NotNull annotated element must not be null
    4. @AssertTrue The annotated element must be true
    5. @AssertFalse The annotated element must be false
    6. @Min (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
    7. @Max (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
    8. @DecimalMin (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
    9. @DecimalMax (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
    10. @Size (max=, min=) the size of the annotated element must be within the specified range
    11. @Digits (integer, fraction) The annotated element must be a number whose value must be within an acceptable range
    12. @Past The annotated element must be a past date
    13. @Future The annotated element must be a future date
    14. @Pattern (regex=,flag=) The annotated element must conform to the specified regular expression
    15. Hibernate Validator Additional constraint
    16. @NotBlank (message =) validation string is not null and must be longer than 0
    17. @Email The annotated element must be an e-mail address
    18. @Length (min=,max=) The size of the annotated string must be within the specified range
    19. @NotEmpty The annotated string must be non-empty
    20. @Range (min=,max=,message=) The annotated element must be within the appropriate range

Can expand on its own.

Usage Example: We accept the form's validation bean as Productunityformatmodel

public class Productunityformatmodel {
Private String productid;//Product ID

Private String categoryId; Product Category ID

Private list<string> categoryname;//Product name

Private String shopcategoryid;//store category ID

@Size (min = 1,max = 50,message = "< title length within 1-50 characters >")
Private String productname;//Product name

Private String productcode;//Product number

@NotNull (message = "< upload at least one photo >")
Private string[] Productimgs;

@NotEmpty (message = "< shipping place is not empty >")
Private String addrprovincename;//Province

@NotEmpty (message = "< shipping provinces and cities are not empty >")
Private String addrprovincecode;//Province code

Private String addrcityname;//City

@NotEmpty (message = "< shipping area is not empty >")
Private String addrcitycode;//City code

@Pattern (regexp = "[0-9]+", message = "< Inventory quantity is pure Digital >")
Private String inventorynum;//Inventory quantity

@Pattern (regexp = "[0-9]+[.]? [0-9]*?, message = "< original price is pure number or decimal >")
Private String marketprice;//Market price

@Pattern (regexp = "[0-9]+[.]? [0-9]*?, message = "< micro-store price is pure digit or decimal >")
Private String saleprice;//Sales price

@NotEmpty (message = "< Product details are not empty >")
Private String productdescription;//Product description

@Pattern (regexp = "[0-9]+[.]? [0-9]*?, message = "< shipping is purely numeric or fractional >")
Private String freight;//Freight

Omit get and Set methods
}


Add @valid annotations to the accepted object of the request method in the controller:

@RequestMapping (value = "/product/savingproductinformation", method = Requestmethod.post)
Public String savingproductinformation (model model, @Valid @ModelAttribute ("Unityformatmodel") Productunityformatmodel Unityformatmodel, bindingresult result) {
if (Result.haserrors ()) {
If validation does not pass, Result.haserrors () is True
Todo somthing--can take some filled data back, easy to echo, provide a friendly point of experience
Return "";//returns to the Request page for field validation failure
}}


On the JSP page (use the other rendering template, check how to use it yourself)

You can use the spring label <form:errors path= "formbean field"/> If the field does not pass validation, this label will display the message you filled in the validation note

Springmvc using Hibernate validator to do field validation

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.