我在GWT2.4中使用Hibernate Validator时遇到问题。当模块启动时,我得到错误:
00:00:05562[错误]延迟绑定结果类型"javax.validation.ValidatorFactory"不应是抽象
java.lang.RuntimeException:的延迟绑定失败"javax.validation.ValidatorFactory"(您是否忘记继承所需模块?)…
模块配置:
<module rename-to='start'>
...
<inherits name="com.google.gwt.validation.Validation" />
<!-- with this doesn't work too
<inherits name="org.hibernate.validator.HibernateValidator" />
-->
...
</module>
入口点:
public class Start implements EntryPoint {
public void onModuleLoad() {
final ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
...
}
}
pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
解决了这个问题
根据http://code.google.com/p/google-web-toolkit/wiki/BeanValidation我必须实现自定义MyValidatorFactory
,并将其添加到项目hibernate验证器sources.jar.中
GWT模块配置:
<!-- hibernate validator -->
<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with class="net.marioosh.gwt.client.MyValidatorFactory">
<when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>
MyValidatorFactory
package net.marioosh.gwt.client;
import javax.validation.Validator;
import net.marioosh.gwt.shared.model.entities.Link;
import com.google.gwt.core.client.GWT;
import com.google.gwt.validation.client.AbstractGwtValidatorFactory;
import com.google.gwt.validation.client.GwtValidation;
import com.google.gwt.validation.client.impl.AbstractGwtValidator;
public class MyValidatorFactory extends AbstractGwtValidatorFactory {
/**
* Only the classes listed in the {@link GwtValidation} annotation can be validated.
* In my example: Link.class can be validated
*/
@GwtValidation(value = { Link.class })
public interface GwtValidator extends Validator {
}
@Override
public AbstractGwtValidator createValidator() {
return GWT.create(GwtValidator.class);
}
}
还需要Hibernate验证器源
<!-- local dependency -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-sources</artifactId>
<version>4.2.0.Final</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/hibernate-validator-4.2.0.Final-sources.jar</systemPath>
</dependency>
使用:
public void onModuleLoad() {
final ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
final Validator validator = factory.getValidator();
...
Link l = new Link(....);
Set<ConstraintViolation<Link>> violations = validator.validate(l);
if(!violations.isEmpty()) {
// validation fail
} else {
// validation success
}
...
}
上次我尝试实现这一功能时,我非常头疼,决定自己实现基于JSR303标准的GWT验证框架。
无论如何,这仍然是一个实验性的框架,正如Wiki中所说:
http://code.google.com/p/google-web-toolkit/wiki/BeanValidation
警告实验性的。API可能会改变。有些事情仍然不起作用
所以,如果我是你,我不会试图让它发挥作用。实现一些能够验证类的生成器。
我也在这里尝试了这个框架:
http://code.google.com/p/gwt-validation/
但再一次,我完全没能做到!但根据你的情况,它可能会很好。。。试着制作一个"非常"小的应用程序来测试不同的方法,看看什么对你有用。