繁体   English   中英

Vaadin 8将String转换为Float

[英]Vaadin 8 convert String to Float

我有3个TextFiled,一个将String保存到数据库,另一个2将FLoat保存到数据库。 当我运行我的应用程序时,出现以下错误:

Property type 'java.lang.Float' doesn't match the field type 'java.lang.String'. Binding should be configured manually using converter.

这是我的代码:

@SpringComponent
@UIScope
public class FuelEditor extends VerticalLayout{
private final FuelReposiotry fuelRepository;

private Fuel fuel;
/* Fields to edit properties in Customer entity */
TextField date = new TextField("Data");
TextField price = new TextField("Cena");
TextField amount = new TextField("Kwota tankowania");

/* Action buttons */
Button save = new Button("Save", FontAwesome.SAVE);
Button cancel = new Button("Cancel");
Button delete = new Button("Delete", FontAwesome.TRASH_O);
CssLayout actions = new CssLayout(save, cancel, delete);

Binder<Fuel> binder = new Binder<>(Fuel.class);

@Autowired
public FuelEditor(FuelReposiotry fuelReposiotry) {
    this.fuelRepository = fuelReposiotry;

    addComponents(date, price, amount, actions);
    // bind using naming convention
    binder.bindInstanceFields(this);

    // Configure and style components
    setSpacing(true);
    actions.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
    save.setStyleName(ValoTheme.BUTTON_PRIMARY);
    save.setClickShortcut(ShortcutAction.KeyCode.ENTER);

    // wire action buttons to save, delete and reset
    save.addClickListener(e -> fuelReposiotry.save(fuel));
    delete.addClickListener(e -> fuelReposiotry.delete(fuel));
    cancel.addClickListener(e -> editFuel(fuel));
    setVisible(false);
}

如何将String转换为Float? 我尝试使用com.vaadin.data.util.converter.StringToIntegerConverter;导入,但是在vaadin 8中,我无法使用转换器。

您应该能够使用StringToFloatConverter ,但是必须手动绑定这些字段:

@SpringComponent
@UIScope
public class FuelEditor extends VerticalLayout{

TextField date = new TextField("Data");
TextField price = new TextField("Cena");
TextField amount = new TextField("Kwota tankowania");

Binder<Fuel> binder = new Binder<>(Fuel.class);

@Autowired
public FuelEditor(FuelReposiotry fuelReposiotry) {

    // Bind float fields manually
    binder.forField(price)
            .withConverter(new StringToFloatConverter("Value must be a float"))
            .bind(Fuel::getPrice, Fuel::setPrice);
    binder.forField(amount)
            .withConverter(new StringToFloatConverter("Value must be a float"))
            .bind(Fuel::getAmount, Fuel::setAmount);
    // bind the last using naming convention
    binder.bindInstanceFields(this);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM