gpt4 book ai didi

java - 如何在 Vaadin 上使用 withConverter 方法防止 Binder 中出现 “null” 文本

转载 作者:行者123 更新时间:2023-12-02 12:11:59 24 4
gpt4 key购买 nike

使用Binder时如何防止显示文本“null” withConverter方法

    TextField id = new TextField("Id");
TextField name = new TextField("Name");

Binder<Customer> b = new Binder<>();

b.forField(id)
.withConverter(Integer::valueOf, String::valueOf, "Invalid")
.bind(Customer::getId, Customer::setId);
b.forField(name)
.bind(Customer::getName, Customer::setName);
b.readBean(customer);

结果是这样的:

enter image description here

How do the conversion without losing the validation type?

最佳答案

最后,我找到了使用vaadin API的解决方案,感谢帮助@Shirkam

    b.forField(id)
.withConverter(Integer::valueOf, String::valueOf, "Invalid")
.withNullRepresentation(0)
.withValidator(new IntegerRangeValidator("Value must be greater than 0 ", 1, null))
.bind(Customer::getId, Customer::setId);
b.forField(name)
.bind(Customer::getName, Customer::setName);
b.readBean(customer);

关于java - 如何在 Vaadin 上使用 withConverter 方法防止 Binder 中出现 “null” 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46472811/

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