ÔÚÈÕ³£¿ª·¢ÖУ¬Éæ¼°µ½ºÜ¶àµÄ×Ö¶ÎÊý¾ÝÑéÖ¤£¬Ò»°ãÆÕͨµÄÑéÖ¤¿ò¼Ü¾Í»ù±¾ÉÏ¿ÉÒÔÂú×ãÈÕ³£µÄ¿ª·¢ÐèÇ󣬵«ÓÐһЩÇé¿ö£¬ÕâЩÆÕͨµÄÑéÖ¤¿ò¼ÜÎÞ·¨´ïµ½ÒªÇó£¬ÀýÈ磬ÎÒÃDZØÐë¶ÔÑéÖ¤µÄÊôÐÔ½øÐзÖ×飬ÓÚÊǾͲúÉúÁËÎÒÃǵÄHibernate-ValidatorÑéÖ¤¿ò¼Ü¡£
Õâ¸öÑéÖ¤¿ò¼Ü£¬Ä¬ÈϵŤ×÷ģʽΪ¿ìËÙʧ°Ü·µ»Øģʽ£¬Ò»µ©·¢ÏÖУÑéʧ°ÜÏÁ¢¼´·µ»Ø¶ÔÓ¦µÄ´íÎóÐÅÏ¢¡£¶øÆÕͨģʽÔÚ²âÊÔʱÆÚ¿ÉÒÔʹÓ㬿ÉÒÔ¶ÔÈ«²¿µÄУÑéÏî½øÐÐÍêÕûµÄУÑ飬ͨ¹ýÐÞ¸ÄÅäÖÃÎļþÖеÄУÑéģʽ£¬´Ó¶øʵÏÖ¹¤×÷ģʽµÄ×ÔÓÉÇл»¡£
¿ª·¢ÈËÔ±ÎÞÐè¹ØÐÄÆäÖеÄÔÀíºÍ¹ý³Ì¡£
Hibernate-ValidatorÑéÖ¤¿ò¼ÜµÄÇ¿´ó¹¦ÄÜ£¬¾ø´ó¶àÊýµÄÇé¿öÏ£¬¶¼ÊÇʹÓÃ×¢½âµÄÐÎʽÀ´Íê³ÉµÄ£¬½ÓÏÂÀ´£¬ÎÒ½«ÁгöHibernate-Validator¿ò¼ÜÖеÄ×¢½âÏ¹©´ó¼Ò²Î¿¼¡£
ValidatorÄÚÖõÄ×¢½â£º
Annotation | Ö§³ÖµÄÊý¾ÝÀàÐÍ | ×÷Óà | Hibernate metadata impact |
@AssertFalse | Boolean, boolean | ÅжϹØÁªÊôÐÔÊÇ·ñΪ²¼¶ûÖµfalse | ûÓÐ |
@AssertTrue | Boolean, boolean | Checks that the annotated element istrue. | ûÓÐ |
@DecimalMax | BigDecimal, BigInteger, String, byte, short, int,long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number. | ±»×¢½âµÄÖµ±ØÐë²»´óÓÚÔ¼ÊøÖÐÖ¸¶¨µÄ×î´óÖµ. Õâ¸öÔ¼ÊøµÄ²ÎÊýÊÇÒ»¸öͨ¹ýBigDecimal¶¨ÒåµÄ×î´óÖµµÄ×Ö·û´®±íʾ. | ûÓÐ |
@DecimalMin | BigDecimal, BigInteger, String, byte, short, int,long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number. | ±»×¢½âµÄÖµ±ØÐ벻СÓÚÔ¼ÊøÖÐÖ¸¶¨µÄ×îСֵ. Õâ¸öÔ¼ÊøµÄ²ÎÊýÊÇÒ»¸öͨ¹ýBigDecimal¶¨ÒåµÄ×îСֵµÄ×Ö·û´®±íʾ. | ûÓÐ |
@Digits(integer=, fraction=) | BigDecimal, BigInteger, String, byte, short, int,long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number. | УÑéÕûÊýλÊýºÍСÊýλÊý | ¶ÔÓ¦µÄÊý¾Ý¿â±í×ֶλᱻÉèÖþ«¶È(precision)ºÍ×¼¶È(scale). |
@Future | java.util.Date, java.util.Calendar; Additionally supported by HV, if the Joda Time date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant. | ¼ì²é¸ø¶¨µÄÈÕÆÚÊÇ·ñ±ÈÏÖÔÚÍí. | ûÓÐ |
@Max | BigDecimal, BigInteger, byte, short, int, longand the respective wrappers of the primitive types. Additionally supported by HV: String(the numeric value represented by a String is eva luated), any sub-type of Number. | ¼ì²é¸ÃÖµÊÇ·ñСÓÚ»òµÈÓÚÔ¼ÊøÌõ¼þÖÐÖ¸¶¨µÄ×î´óÖµ. | »á¸ø¶ÔÓ¦µÄÊý¾Ý¿â±í×Ö¶ÎÌí¼ÓÒ»¸öcheckµÄÔ¼ÊøÌõ¼þ. |
@Min | BigDecimal, BigInteger, byte, short, int, longand the respective wrappers of the primitive types. Additionally supported by HV: String(the numeric value represented by a String is eva luated), any sub-type of Number. | ¼ì²é¸ÃÖµÊÇ·ñ´óÓÚ»òµÈÓÚÔ¼ÊøÌõ¼þÖй涨µÄ×îСֵ. | »á¸ø¶ÔÓ¦µÄÊý¾Ý¿â±í×Ö¶ÎÌí¼ÓÒ»¸öcheckµÄÔ¼ÊøÌõ¼þ. |
@NotNull | Any type | Checks that the annotated value is notnull. | ¶ÔÓ¦µÄ±í×ֶβ»ÔÊÐíΪnull. |
@Null | Any type | Checks that the annotated value is null. | ûÓÐ |
@Past | java.util.Date, java.util.Calendar; Additionally supported by HV, if the Joda Time date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant. | ¼ì²é×¢½â¶ÔÏóÖеÄÖµ±íʾµÄÈÕÆڱȵ±Ç°Ôç. | ûÓÐ |
@Pattern(regex=, flag=) | String | ¼ì²é¸Ã×Ö·û´®ÊÇ·ñÄܹ»ÔÚmatchÖ¸¶¨µÄÇé¿öϱ»regex¶¨ÒåµÄÕýÔò±í´ïʽƥÅä. | ûÓÐ |
@Size(min=, max=) | String, Collection, Map and arrays | УÑé¶ÔÏóµÄsize¡£±¾ÎÄ×÷ÕßÈÏΪǰÌáÊǸöÔÏóÓÐsize()·½·¨£¬String³ýÍâ¡£ | ¶ÔÓ¦µÄÊý¾Ý¿â±í×ֶεij¤¶È»á±»ÉèÖóÉÔ¼ÊøÖж¨ÒåµÄ×î´óÖµ. |
@Valid | Any non-primitive type | µÝ¹éµÄ¶Ô¹ØÁª¶ÔÏó½øÐÐУÑé, Èç¹û¹ØÁª¶ÔÏóÊǸö¼¯ºÏ»òÕßÊý×é, ÄÇô¶ÔÆäÖеÄÔªËؽøÐеݹéУÑé,Èç¹ûÊÇÒ»¸ömap,Ôò¶ÔÆäÖеÄÖµ²¿·Ö½øÐÐУÑé. | ûÓÐ |
Hibernate ValidatorÍØÕ¹µÄ×¢½â£º
Annotation | Ö§³ÖµÄÊý¾ÝÀàÐÍ | ×÷Óà | Hibernate metadata impact |
@CreditCardNumber | String | УÑéÐÅÓÿ¨ºÅÂë | ûÓÐ |
String | УÑéÓʼþµØÖ· | ûÓÐ | |
@Length(min=, max=) | String | ¹¦ÄÜͬ@Size£¬µ«ÊÇÖ»Ö§³ÖStringÀàÐÍ | ¶ÔÓ¦µÄÊý¾Ý¿â±í×ֶεij¤¶È»á±»ÉèÖóÉÔ¼ÊøÖж¨ÒåµÄ×î´óÖµ. |
@NotBlank | String | ²»Îªnull£¬²»Îª¿ÕÖµ£¬²»ÎªÈ«¿Õ¸ñ¡£¹¦ÄÜÇ¿´óÓÚ@NotEmpty | ûÓÐ |
@NotEmpty | String,Collection,Map and arrays | УÑéÊÇ·ñΪnull»òÕßΪ¿ÕÖµ¡£¹¦ÄÜÇ¿ÓÚ@NotNull | ûÓÐ |
@Range(min=, max=) | BigDecimal,BigInteger,String, byte,short, int,long and the respective wrappers of the primitive types | ÅжÏÊýÖµµÄ·¶Î§£¬²»½öÖ§³ÖÊýÖµÀàÐÍ£¬»¹Ö§³Ö×Ö·û´®¡¢×ֽڵȵÈÀàÐÍ | ûÓÐ |
@SafeHtml(whitelistType=, additionalTags=) | CharSequence | ÎÞʹÓüÛÖµ | ûÓÐ |
@ScriptAssert(lang=, script=, alias=) | Any type | ÎÞʹÓüÛÖµ | ûÓÐ |
@URL(protocol=, host=, port=, regexp=, flags=) | String | Checks if the annotated string is a valid URL according to RFC2396. If any of the optional parameters protocol, host or port are specified, the corresponding URL fragments must match the specified values. The optional parametersregexp and flags allow to specify an additional regular expression (including regular expression flags) which the URL must match. | ûÓÐ |
½ÓÏÂÀ´£¬ÑÝʾһÏ£¬ÔÚSpringMVCÖУ¬Ê¹ÓÃValidatorÑéÖ¤¿ò¼ÜÀ´½øÐÐÊý¾ÝµÄÑéÖ¤¡£
¿´Í¼£º
ÔÚʵÌåBeanÖУ¬¸øÐèÒªÑéÖ¤µÄÊôÐÔ£¬ÌùÉÏ×¢½â¾Í¿ÉÒÔÁË¡£
ÄÇôÈçºÎÑéÖ¤ÄØ£¿
»¹ÊÇSpringMVCÀ´ÑÝʾ£¬ÔÚ½ÓÊÕ²ÎÊýµÄʱºò£¬ÕâôÀ´£º
Êǵģ¬ÔÚ½ÓÊÕ²ÎÊýµÄʱºò£¬SpringMVC»á°ÑÊý¾Ý·â×°³ÉÖ¸¶¨µÄʵÌåÀà¶ÔÏ󣬶øÎÒÐèÒª¶ÔÕâ¸öʵÌåÀà¶ÔÏó·â×°µÄÊý¾Ý½øÐÐÑéÖ¤£¬ËùÒÔ£¬¸øÕâ¸öʵÌåÀà¶ÔÏóÌùÉÏ@Validated×¢½â£¬Èç¹ûÑéÖ¤Óв»·ûºÏµÄÊôÐÔ£¬¾Í»á°Ñ×¢½âÖжÔÓ¦µÄmessageÐÅÏ¢£¬·â×°³ÉBindingResultÀ࣬ͨ¹ýÕâ¸öÀà¶ÔÏ󣬿ÉÒÔ»ñÈ¡µ½¶ÔÓ¦µÄ´íÎóÐÅÏ¢£¡£¡£¡
²»Í¬µÄ¿ò¼Ü£¬»ñÈ¡ValidatorÑéÖ¤µÄ´íÎóÐÅÏ¢µÄ·½Ê½¶¼²»¾¡Ïàͬ£¬ÕâÀï¾Í²»Ò»Ò»Áгö¡£
ºÃÁË£¬Validator×¢½âÑéÖ¤¿ò¼ÜµÄ½éÉܵ½´Ë£¡£¡£¡
END£¡£¡£¡