提问人:Curi 提问时间:9/22/2023 最后编辑:Curi 更新时间:9/25/2023 访问量:63
如何将空的 Vaadin TextField 映射到模型属性中的 null 值
How to map empty Vaadin TextField to null value in model property
问:
Vaadin v24 的默认行为是将空 TextField 映射到空 String 作为模型值。如何将其映射到所有TextFields的null值,而无需将withNullRepresentation添加到每个textField绑定?
我已经制作了自己的 CustomBinder,我想知道这是正确的方法。代码如下:
public class CustomBinder<BEAN> extends Binder<BEAN> {
private static final long serialVersionUID = 3555096816850091643L;
public CustomBinder() {
super();
}
public CustomBinder(Class<BEAN> p_beanType, boolean p_scanNestedDefinitions) {
super(p_beanType, p_scanNestedDefinitions);
}
public CustomBinder(PropertySet<BEAN> p_propertySet) {
super(p_propertySet);
}
public CustomBinder(Class<BEAN> p_class) {
super(p_class);
}
@Override
public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forField(HasValue<?, FIELDVALUE> p_field) {
BindingBuilder<BEAN, FIELDVALUE> forFieldBinding = super.forField(p_field);
if (p_field instanceof TextField || p_field instanceof TextArea) {
forFieldBinding = forFieldBinding.withNullRepresentation((FIELDVALUE) "");
}
return forFieldBinding;
}
}
答: 暂无答案
评论