在 GWT 中使用自定义 javax 验证注释
对于当前项目,我们使用 JSR-303 注释来验证我们的接口参数。
我需要为日期创建自定义注释,因为默认的 @Past 和 @Before 也考虑了时间。
这是我的注释的定义:
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { NotInFutureValidator.class })
/**
* Annotated element must be a date before tomorrow, compared to the system's date
*/
public @interface NotInFuture {
String message() default "{be.credoc.contractRegistration.interface_v2.validation.pastignoretime}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
验证器的实现非常简单。
我们的 Web 应用程序是用 GWT 编写的,并且使用了 gwt-validation。这允许我们通过相同的注释在客户端验证我们的参数。
但是,当我使用自定义注释来注释我的参数并且我想通过使用 GwtValidatorFactory 来验证它时,当输入日期是将来的日期时,它不会给我错误。
有没有人有在 GWT 应用程序中定义和使用自己的注释的经验,并且可以看到我缺少的内容?
提前致谢
For the current project we're using JSR-303 annotations to validate our interface parameters.
I needed to create a custom annotation for dates, as the default @Past and @Before also take into account the time.
This is the definition of my annotation:
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { NotInFutureValidator.class })
/**
* Annotated element must be a date before tomorrow, compared to the system's date
*/
public @interface NotInFuture {
String message() default "{be.credoc.contractRegistration.interface_v2.validation.pastignoretime}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The implementation of the validator is pretty straight forward.
Our webapplication is written in GWT and it makes use of gwt-validation. This allows us to validate our parameters on client side by means of the same annotations.
However, when I annotate my parameters with my custom annotation and I want to validate it by making use of the GwtValidatorFactory, it doesn't give me an error when the input date is in the future.
Does anyone have experiencing defining and using their own annotations in a GWT application and can see what I'm missing?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试通过提供如下所示的 XML 约束映射来重新定义现有
@Past
和@Future
注释的验证器,而不是创建新注释:此映射必须是在配置文件
META-INF/validation.xml
中注册,如下所示:您可以在 Bean Validation 规范 和 Hibernate 验证器 参考指南。
Instead of creating new annotations you might try to re-define the validators for the existing
@Past
and@Future
annotations by providing an XML constraint mapping like this:This mapping must be registered in the configuration file
META-INF/validation.xml
like this:You can learn more about custom validators for existing constraints in the Bean Validation specification and the Hibernate Validator reference guide.
显然,我们为每个自定义注释编写了自己的 JavaScript 实现,因此本例中的解决方案是编写这样的实现。
Apparently we wrote our own JavaScript implementations for each custom annotation, so the solution in this case was to write such an implementation.