1

我尝试在 GWT 中使用 bean 验证。我遵循本指南: https ://developers.google.com/web-toolkit/doc/latest/DevGuideValidation

在编译时,我有以下错误:

ERROR: Could not load deferred binding result type 'com.google.gwt.sample.validation.client.SampleValidatorFactory'

如果我包含这个依赖项,我可以让它工作:

<dependency>
  <groupId>com.googlecode.gwt-validation</groupId>
  <artifactId>gwt-validation</artifactId>
  <version>2.1</version>
</dependency>

但我觉得这很奇怪,因为指南中没有提到这种依赖关系。

问题:由于 GWT 2.5 应该支持 bean 验证,为什么我需要这个额外的库?我究竟做错了什么?

我正在使用 GWT 2.5.0

我的 pom.xml 包含以下依赖项:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.3.1.Final</version>
  <type>jar</type>
  <classifier>sources</classifier>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.3.1.Final</version>
  <type>jar</type>
</dependency>

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <type>jar</type>
  <classifier>sources</classifier>
</dependency>

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.0.0.GA</version>
  <type>jar</type>
</dependency>

我的 gwt.xml 包含以下几行:

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

在代码中,我以这种方式获取验证器:

import javax.validation.Validator;
...
private static final Validator VALIDATOR= Validation.buildDefaultValidatorFactory().getValidator();
4

1 回答 1

2

您正在将第三方库 gwt-validation 与 Native GWT Validation 混合使用。gwt-validation 不是来自官方 gwt 团队或谷歌。如果您打算坚持本机 gwt 验证支持,则不应在您的 pom 中使用以下内容。

<dependency>
  <groupId>com.googlecode.gwt-validation</groupId>
  <artifactId>gwt-validation</artifactId>
  <version>2.1</version>
</dependency>

另请注意,您的本地 gwt hibernate 验证器仅支持 4.1.0。GWT 示例用法。GWT 原生休眠验证器源代码

您可以将其包含为

<!-- Hibernate bean validation binary for the server -->

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.1.0.Final</version>
  <exclusions>
    <exclusion>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- Hibernate bean validation source for the GWT client -->

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.1.0.Final</version>
  <classifier>sources</classifier>
  <exclusions>
    <exclusion>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
    </exclusion>
    <exclusion>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
    </exclusion>
  </exclusions>
</dependency>

<!-- Required by Hibernate validator because slf4j-log4j is
     optional in the hibernate-validator POM
 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>
于 2013-04-04T17:30:34.563 回答