如何使用gwt-2.4.0安装gwt-validation

时间:2012-01-26 09:58:24

标签: gwt gwt-validation

我想将Gwt-Validation与Gwt 2.4.0一起使用。

我的gwt.xml文件中有:

  <inherits name="javax.validation.Validation" />
  <inherits name="com.google.gwt.validation.Validation" />
  <inherits name='com.em.validation.Validation' />

与文档中一样,我有以下代码:

import javax.validation.Validator;
import javax.validation.ValidatorFactory;
...

ValidatorFactory factory =
    Validation.byDefaultProvider().configure().buildValidatorFactory();

Validator validator = factory.getValidator();

问题是我有以下错误:

  

延迟绑定结果类型'javax.validation.ValidationFactory'不应该是抽象的。

     

// ...美丽的堆栈跟踪主要是:

     

'javax.validation.ValidatorFactory'的延迟绑定失败(做了   你忘了继承一个必需的模块吗?)

我的pom.xml包含:

<dependency>
  <groupId>com.googlecode.gwt-validation</groupId>
  <artifactId>gwt-validation</artifactId>
  <version>2.0</version>
</dependency>
<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <classifier>sources</classifier>
  <scope>provided</scope>
</dependency>

我该怎么办? 感谢。

2 个答案:

答案 0 :(得分:1)

问题出在java进口中。

在以下代码中:

ValidatorFactory factory =
    Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();

我导入了:

com.google.gwt.validation.client代替javax.validation

java编译时没有错误!只是gwt编译时的上述错误(或托管模式的运行时)。

答案 1 :(得分:0)

我在GWT.XML文件中有这个:

<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with class="com.example.client.MyValidatorFactory">
    <when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>

我像这样定义了MyValidatorFactory:

public final class MyValidatorFactory extends AbstractGwtValidatorFactory {

    /**
     * Validator marker for the Validation Sample project. Only the classes
     * listed in the {@link GwtValidation} annotation can be validated.
     */
    @GwtValidation(value = { Customer.class, License.class, Account.class })
    public interface MyValidator extends Validator {
    }

    @Override
    public AbstractGwtValidator createValidator() {
        return GWT.create(GwtValidator.class);
    }
}

在我的类路径中,我有:

hibernate-validator-4.2.0.Final-sources.jar
hibernate-validator-4.2.0.Final.jar

现在您可以验证您的域对象:

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Account>> violations = validator.validate(account);

同样的验证然后也适用于服务器端

private static ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
Set<ConstraintViolation<DomainResource>> violations = factory.getValidator().validate(account);

好东西!