Java Vaadin 8将字符串转换为浮点

Java Vaadin 8将字符串转换为浮点,java,spring-boot,vaadin8,Java,Spring Boot,Vaadin8,我有3个textfield,一个将字符串保存到数据库,还有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 FuelE

我有3个textfield,一个将字符串保存到数据库,还有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);
}
@SpringComponent
@望远镜
公共类FuelEditor扩展了垂直布局{
私人最终fuelRepository fuelRepository;
私人燃料;
/*在客户实体中编辑属性的字段*/
TextField日期=新的TextField(“数据”);
TextField价格=新TextField(“Cena”);
TextField金额=新TextField(“Kwota tankowania”);
/*动作按钮*/
按钮保存=新建按钮(“保存”,Fontsome.save);
按钮取消=新按钮(“取消”);
按钮删除=新按钮(“删除”,FontAwesome.TRASH_O);
CssLayout actions=新建CssLayout(保存、取消、删除);
粘结剂=新粘结剂(燃料等级);
@自动连线
公共FuelEditor(FuelReposiotry FuelReposiotry){
this.fuelRepository=fuelReposiotry;
添加组件(日期、价格、金额、操作);
//使用命名约定绑定
binder.bindInstanceFields(本);
//配置和设置组件的样式
设置间隔(真);
actions.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(快捷操作.KeyCode.ENTER);
//连接用于保存、删除和重置的操作按钮
save.addClickListener(e->fuelReposiotry.save(fuel));
delete.addClickListener(e->fuelReposiotry.delete(fuel));
取消。添加ClickListener(e->editFuel(fuel));
setVisible(假);
}

如何将字符串转换为浮点?我正在尝试导入com.vaadin.data.util.converter.StringToIntegerConverter;,但在vaadin 8中,我无法使用converter。

您应该能够使用
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);
}
@SpringComponent
@望远镜
公共类FuelEditor扩展了垂直布局{
TextField日期=新的TextField(“数据”);
TextField价格=新TextField(“Cena”);
TextField金额=新TextField(“Kwota tankowania”);
粘结剂=新粘结剂(燃料等级);
@自动连线
公共FuelEditor(FuelReposiotry FuelReposiotry){
//手动绑定浮点字段
活页夹forField(价格)
.withConverter(新的StringToFloatConverter(“值必须是浮点”))
.bind(Fuel::getPrice,Fuel::setPrice);
活页夹forField(金额)
.withConverter(新的StringToFloatConverter(“值必须是浮点”))
.bind(Fuel::getAmount,Fuel::setAmount);
//使用命名约定绑定最后一个
binder.bindInstanceFields(本);
}