SPRINGMVC Validation Framework validation custom annotations for passing parameters to an internationalized resource file

Source: Internet
Author: User

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

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.