gpt4 book ai didi

gwt - 如何使用客户端验证器验证方法调用的返回值在 EditorDriver 上设置约束条件

转载 作者:行者123 更新时间:2023-12-04 17:23:45 24 4
gpt4 key购买 nike

使用 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>>)

我能找到的唯一有点相关的帖子是 Issue 6270 !

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

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

类(class)



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();
}
}

绑定(bind)器

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
  • 最佳答案

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

    Set<?> test = violations; 
    editorDriver.setConstraintViolations((Set<ConstraintViolation<?>>) test);

    关于gwt - 如何使用客户端验证器验证方法调用的返回值在 EditorDriver 上设置约束条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14878501/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com