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:
- Dbean built-in constraint in Validation
- @Null The annotated element must be Null
- @NotNull annotated element must not be null
- @AssertTrue The annotated element must be true
- @AssertFalse The annotated element must be false
- @Min (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
- @Max (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
- @DecimalMin (value) The annotated element must be a number whose value must be greater than or equal to the specified minimum value
- @DecimalMax (value) The annotated element must be a number whose value must be less than or equal to the specified maximum value
- @Size (max=, min=) the size of the annotated element must be within the specified range
- @Digits (integer, fraction) The annotated element must be a number whose value must be within an acceptable range
- @Past The annotated element must be a past date
- @Future The annotated element must be a future date
- @Pattern (regex=,flag=) The annotated element must conform to the specified regular expression
- Hibernate Validator Additional constraint
- @NotBlank (message =) validation string is not null and must be longer than 0
- @Email The annotated element must be an e-mail address
- @Length (min=,max=) The size of the annotated string must be within the specified range
- @NotEmpty The annotated string must be non-empty
- @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