5

使用 GWT 2.5.0,我想使用客户端验证和编辑器。尝试将 ConstraintViolation java.util.Set 传递给 EditorDriver 时遇到以下错误,如下所示。

Validator a = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Person>> b = a.validate(person);
editorDriver.setConstraintViolations(b);

The method setConstraintViolations(Iterable<ConstraintViolation<?>>) in the type EditorDriver<Person> is not applicable for the arguments (Set<ConstraintViolation<Person>>)

我能找到的唯一相关的帖子是问题 6270

下面是一个带有 Person Editor 的 PopUpDialog 示例,它允许您指定名称并根据您的注释对其进行验证。注释掉personDriver.setConstraintViolations(violations);PersonEditorDialog 中的行将允许您运行示例。

我没有足够的声誉点来发布示例的图像。

课程


public class Person {

@NotNull(message = "You must have a name")

@Size(min = 3, message = "Your name must contain more than 3 characters")

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

人物编辑器对话框

public class PersonEditorDialog extends DialogBox implements Editor<Person> {

private static PersonEditorDialogUiBinder uiBinder = GWT
        .create(PersonEditorDialogUiBinder.class);

interface PersonEditorDialogUiBinder extends
        UiBinder<Widget, PersonEditorDialog> {
}

private Validator validator;

public PersonEditorDialog() {
    validator = Validation.buildDefaultValidatorFactory().getValidator();
    setWidget(uiBinder.createAndBindUi(this));
}

interface Driver extends SimpleBeanEditorDriver<Person, PersonEditorDialog> {
};

@UiField
ValueBoxEditorDecorator<String> nameEditor;

@UiField
Button validateBtn;

private Driver personDriver;

@UiHandler("validateBtn")
public void handleValidate(ClickEvent e) {
    Person created = personDriver.flush();
    Set<ConstraintViolation<Person>> violations = validator
            .validate(created);
    if (!violations.isEmpty() || personDriver.hasErrors()) {
        StringBuilder violationMsg = new StringBuilder();
        for (Iterator<ConstraintViolation<Person>> iterator = violations.iterator(); iterator.hasNext();) {
            ConstraintViolation<Person> constraintViolation = (ConstraintViolation<Person>) iterator
                    .next();
            violationMsg.append(constraintViolation.getMessage() + ",");
        }
        Window.alert("Detected violations:" + violationMsg);
         personDriver.setConstraintViolations(violations);
    }
}

@Override
public void center() {
    personDriver = GWT.create(Driver.class);
    personDriver.initialize(this);
    personDriver.edit(new Person());
    super.center();
}
}

样本验证工厂

public final class SampleValidationFactory 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);
}
}

EditorValidationTest

public class EditorValidationTest implements EntryPoint {


/**
 * This is the entry point method.
 */
public void onModuleLoad() {
    PersonEditorDialog personEditorDialog = new PersonEditorDialog();
    personEditorDialog.center();
}
}

绑定器

PersonEditorDialog.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:e="urn:import:com.google.gwt.editor.ui.client">
<ui:style>
    .important {
        font-weight: bold;
    }
</ui:style>
<g:HTMLPanel>
    <g:Label>Enter your Name:</g:Label>
    <e:ValueBoxEditorDecorator ui:field="nameEditor">
        <e:valuebox>
            <g:TextBox />
        </e:valuebox>
    </e:ValueBoxEditorDecorator>
    <g:Button ui:field="validateBtn">Validate</g:Button>
</g:HTMLPanel>
</ui:UiBinder> 

GWT 模块

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"
"http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='editorvalidationtest'>
<inherits name='com.google.gwt.user.User' />
<inherits name='com.google.gwt.user.theme.clean.Clean' />
<inherits name="com.google.gwt.editor.Editor"/>

<!-- Validation module inherits -->

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

<!-- Specify the app entry point class. -->
<entry-point class='com.test.client.EditorValidationTest' />

<!-- Specify the paths for translatable code -->
<source path='client' />
<source path='shared' />

</module>

类路径所需的库

  • hibernate-validator-4.1.0.Final.jar
  • hibernate-validator-4.1.0.Final-sources.jar
  • validation-api-1.0.0.GA.jar(在 GWT SDK 中)
  • validation-api-1.0.0.GA-sources.jar(在 GWT SDK 中)
  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar
  • log4j-1.2.16.jar
4

2 回答 2

7

正如评论中所讨论的,以下演员被确定为有效的解决方法。

Set<?> test = violations; 
editorDriver.setConstraintViolations((Set<ConstraintViolation<?>>) test);
于 2013-02-19T14:36:21.910 回答
3

这就是我一遍又一遍地做的事情:

    List<ConstraintViolation<?>> adaptedViolations = new ArrayList<ConstraintViolation<?>>();
    for (ConstraintViolation<Person> violation : violations) {
        adaptedViolations.add(violation);
    }
    editorDriver.setConstraintViolations(adaptedViolations);

驱动程序定义了通配符泛型类型,您不能传入类型约束违规。

于 2013-02-14T17:23:41.233 回答