2011-10-06 37 views
-1

对于当前的项目,我们使用JSR-303注释来验证我们的接口参数。在GWT中使用自定义的javax验证注释

我需要为日期创建自定义注释,因为默认@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验证。这允许我们通过相同的注释在客户端验证我们的参数。

但是,当我用我的自定义注释注释我的参数,并且我想通过使用GwtValidatorFactory来验证它时,它在输入日期未来时不会给我一个错误。

有没有人遇到过在GWT应用程序中定义和使用自己的注释,并可以看到我失踪的内容?

在此先感谢

回答

0

显然我们为每个自定义注释编写了自己的JavaScript实现,因此这种情况下的解决方案是编写这样的实现。

1

而不是创造新的注解,你可以尝试通过提供这样一个XML约束映射重新定义为现有@Past@Future注解验证:

<?xml version="1.0" ?> 
<constraint-mappings 
    xmlns="http://jboss.org/xml/ns/javax/validation/mapping" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation= 
     "http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"> 

    <constraint-definition annotation="javax.validation.constraints.Past"> 
     <validated-by include-existing-validators="false"> 
      <value>x.y.z.NotInFutureValidator</value> 
     </validated-by> 
    </constraint-definition> 
</constraint-mappings> 

这种映射必须在配置文件META-INF/validation.xml中注册如下:

<?xml version="1.0" encoding="UTF-8"?> 
<validation-config 
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd"> 

    <constraint-mapping>META-INF/validation/custom-constraints.xml</constraint-mapping> 

</validation-config> 

你c了解更多关于Bean验证specification和Hibernate验证器reference guide中现有约束的自定义验证器。