GWT JSR 303 client validation

时光怂恿深爱的人放手 提交于 2019-12-18 11:28:05

问题


I'm developing a GWT application with a Spring backend that uses JSR 303 validation. The application can go offline and use the browser HTML5/Gears database instead.

What is the best way to implement the client validation? So far I have found gwt-validation framework (http://code.google.com/p/gwt-validation/) but it seems it is no longer active.

Thanks!

UPDATE:

There is a new GWT official project to support JSR 303 Bean Validation (link here). IMO this is the way to go once the project is mature enough.


回答1:


Our validation framework is a client and server-side data input validation framework. Its roles is to ensure business rules compliance of data passed from the clients to the server.

The validation framework uses the GWT Validation project which implements the "JSR 303: Bean Validation" specification.

The idea is to decorate Data Transfer Objects (DTO) classes and fields with JSR303 annotations to describe their validity rules.

  1. Each Data Transfer Objects must be decorated with its own validation annotations.
  2. Each server-side service implementation must validate Data Transfer Objects it receives from the client.

On the client side, to use GWT-Validation in your project you'll need to add (along with the jar on your classpath) to your GWT module xml file

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

Ensure DTOs implement com.google.gwt.validation.client.interfaces.IValidatable

To validate on the client side use

com.google.gwt.validation.client.interfaces.IValidator.validateProperty((T) model, propertyName);

On the server side use

com.google.gwt.validation.server.ServerValidator

It's a bit of work to set this up properly but then it works perfectly.




回答2:


GWT 2.5 has a new feature just for this : https://developers.google.com/web-toolkit/doc/latest/DevGuideValidation

It uses the Hibernate Validator.

1) You will need to extend AbstractGwtValidatorFactory and apply your bean e.g. :

public final class SampleValidatorFactory extends AbstractGwtValidatorFactory {

  /**
   * Validator marker for the Validation Sample project. Only the classes and groups listed
   * in the {@link GwtValidation} annotation can be validated.
   */
  @GwtValidation(Person.class)
  public interface GwtValidator extends Validator {
  }

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

2) Then add this to your gwt.xml :

<inherits name="org.hibernate.validator.HibernateValidator" />
<replace-with
  class="yourpackage.SampleValidatorFactory">
  <when-type-is class="javax.validation.ValidatorFactory" />
</replace-with>

3) Validate your bean

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



回答3:


I haven't used it yet, but GWT 2.1 includes some Validation support.

Check the ShowCase example.



来源:https://stackoverflow.com/questions/4247045/gwt-jsr-303-client-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!