The use of the SPRINGMVC verification Framework validation is not the focus of this article, see the SPRINGMVC introduction to the validation
When using validation, a friend must have encountered a problem, that is: Unable to pass parameters to the International resource file properties error description.
As an example:
In the user class
@NotEmpty (message= "{password.empty.error}") private String password;
In the resource file validation_zh_cn.properties
Password.empty.error=password cannot be empty
In the actual development, many parameters are to verify the non-null, if each parameter is a separate error description, it is very troublesome. Properties support the notation of "{}", but using JSR-303 annotations cannot implement pass parameters. I thought of a way to do this by customizing annotations.
First, build a custom @notempty annotation:
Package Com.itkt.payment.core.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import Java.lang.annotation.Target; Import Javax.validation.constraint;import Javax.validation.payload;import Com.itkt.payment.core.handler.NotEmptyValidator, @Retention (retentionpolicy.runtime) @Target ({Elementtype.method , Elementtype.field, Elementtype.annotation_type, Elementtype.constructor, Elementtype.parameter}) @Constraint ( Validatedby = {Notemptyvalidator.class}) public @interface notempty {String field () default ""; String message () default "{Com.itkt.payment.core.handler.NotEmpty.message}"; Class<?>[] Groups () default {}; class<? Extends payload>[] Payload () default {};}
In our custom notempty annotations, we have added field fields to identify the names of the fields.
Then, build the Notnullvalidator implementation class:
Package Com.itkt.payment.core.handler;import Javax.validation.constraintvalidator;import Javax.validation.constraintvalidatorcontext;import Com.itkt.payment.core.annotation.notnull;public Class Notnullvalidator implements Constraintvalidator<notnull, object> {@Overridepublic void Initialize (notnull annotation) {} @Overridepublic boolean isValid (Object str, constraintvalidatorcontext constraintvalidatorcontext) { return str! = NULL;}}
Then, in the resource file validation_zh_cn.properties, change the wording:
Password.empty.error={field} cannot be empty
Finally, we can use the custom notempty annotations in the user class:
@NotEmpty (field = "Password", message = "{Password.empty.error}") private String password;
In fact, the internationalized resource file itself supports getting the parameter values of the property from the JSR-303 annotation, for example from the @length annotation, getting the values of the Min and Max properties:
Username.length.error= user name length must be between {Min}-{max}
The @notempty annotations cannot be implemented because none of the properties can pass the field names, so the custom @notempty annotations extend the field fields.
SPRINGMVC Validation Framework validation custom annotations for passing parameters to an internationalized resource file