Vaadin 8 将字符串转换为浮点数
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?我尝试使用 import 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);
}
我有 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?我尝试使用 import 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);
}