A comparison of @valid and validated
@Valid is used when using hibernate validation
@Validated is used only with the spring Validator check mechanism
One: @Validated is only used with the spring Validator check mechanism
The @Validated and Bindingresult Bindingresult are paired and the formal parameter order is fixed (one after the other).
For example:
@ModelAttribute ("Student") is a new student () class created to verify that the data student submitted by the foreground is correct (@Validated student student)
@RequestMapping (value = "/addstudent", method = requestmethod.post) public String addstudent (@ModelAttribute (" Student ") @Validated student student, Bindingresult Bindingresult, model model) { if (bindingresult.haserrors ( ) { return "student"; } Model.addattribute ("Name", Student.getname ()); Model.addattribute ("Age", Student.getage ()); Model.addattribute ("id", Student.getid ()); return "Student_result"; }
Data submitted by the front desk:
Front desk commandname= "Student" Bound student class
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><%@ taglib uri=" http://www.springframework.org/tags/form "prefix=" form " %><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%><%@ page iselignored= "false"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Full code:
Import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.stereotype.controller;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.bind.webdatabinder;import Org.springframework.web.bind.annotation.initbinder;import Org.springframework.web.bind.annotation.modelattribute;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.ui.model;import Org.springframework.validation.bindingresult;import Org.springframework.validation.validator;import org.springframework.validation.annotation.Validated; @Controllerpublic class Studentcontroller {@Autowired @Qualifier ("Studentvalidator") Private Valida Tor Validator; @InitBinder private void Initbinder (Webdatabinder binder) {binder.setvalidator (validator); } Front-end from data reference: CommandName @ModelAttribute ("student") public student Createstudentmodel () { return new Student (); } @RequestMapping (value = "/student", method = requestmethod.get) public Modelandview student () { return new Modelandview ("Student", "command", New Student ()); } @RequestMapping (value = "/addstudent", method = requestmethod.post) public String addstudent (@Model Attribute ("student") @Validated student student, Bindingresult Bindingresult, model model) {if (Bindin Gresult.haserrors ()) {return "student"; } model.addattribute ("Name", Student.getname ()); Model.addattribute ("Age", Student.getage ()); Model.addattribute ("id", Student.getid ()); return "Student_result"; } }
Two: @Valid is used when using hibernate validation
@Valid
Take a look at the following dependencies and know that Java's JSR303 declares such interfaces, and then hibernate-validator implements them.
<dependency> <groupId>javax.validation</groupId> <artifactid>validation-api </artifactId> <version>1.1.0.Final</version> </dependency> <dependency > <groupId>org.hibernate</groupId> <artifactid>hibernate-validator</artifactid > <version>5.2.1.Final</version> </dependency>
JSR303 the type of validation defined
Null check @null Verify that the object is null@notnull to verify that the object is not null, that a string of length 0 cannot be checked to check if the constraint string is Null or if the length of the trim is greater than 0, the string only, and the front and back spaces are removed. @No Tempty checks whether the constraint element is null or empty. Booelan checks @asserttrue to verify that the Boolean object is True @AssertFalse verifies that the Boolean object is a false length check @size (min=, max=) Validation object (Array, collection,map,string) length within a given range @Length (min=, max=) validates that the annotated String is between Min and Max include D. Date check @past Verify that the date and calendar objects are @Future before the current time to verify that the date and Calendar objects @Pattern validate the String after the current time It is recommended to use the Stirng,integer type, which is not recommended on the type of int, because the form value is "" cannot be converted to int, but can be converted to stirng to "", Integer is null@min to verify Num The BER and string objects are large equal to the specified value @Max verify that number and string objects are small equal to the specified value @DecimalMax the value being dimensioned 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 to be dimensioned must be not less than the minimum specified in the constraint. The parameter of this constraint is a string representation of the minimum value defined by BigDecimal. Decimal Presence Precision @digits verifies that the composition of number and string is legitimate @Digits (integer=,fraction=) verifies that the string is compliant with the specified lattice Interger Specifies the integer precision, fraction specifies the decimal precision. @Range (min=, max=) ChEcks whether the annotated value lies between (inclusive) the specified minimum and maximum. @Range (min=10000,max=50000,mes Sage= "Range.bean.wage") private BigDecimal wage; 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 to perform recursive validation) @CreditCardNumber credit card verification @email Verify that it is an e-mail address, and if it is null, validation is passed. @ScriptAssert (lang=, script=, alias=) @URL (protocol=,host=, port=,regexp=, flags=)
For example:
public class user{ @NotNull @Length (min = 1,max = 5) private String name; @Size (min=1,max=5) private int Pasword }
@RequestMapping (value = "/create", method = Requestmethod.post) @ResponseBody blackdisck createUser (@ Modelattribute @Valid User User, bindingresult result) { if (result.haserrors ()) { list<objecterror> errorlist = Result.getallerrors (); for (Objecterror error:errorlist) { System.out.println (Error.getcode () + "msg=" + error.getdefaultmessage ()); } return null; } .......... }
SPRINGMVC's @validated/@Valid annotated use and Bindingresult Bindingresult