其實Vaadin行爲正確,但它遵循幅度規則(已被約根德拉·辛格提到的),請看到一個example
![enter image description here](https://i.stack.imgur.com/4rOoB.png)
也請檢查以下內容:
float value1 = 12.0f;
float value2 = 123123123;
BeanItem<Float> item1 = new BeanItem<Float>(value1);
BeanItem<Float> item2 = new BeanItem<Float>(value2);
System.out.println(" result 1: " + item1.getBean());
System.out.println(" result 2: " + item2.getBean());
結果:
result 1: 12.0
result 2: 1.2312312E8
所以正確的解決方案(如我所見)如下所示:
- 定義您自己的Bean。沒有理由讓BeanItem包裝一個浮點值。
- 定義您的PropertyFormatter(example)
- 重要的是要注意,一個不應該返回一個字符串,而不是正確的數據類型。它會影響編輯,驗證等
PropertyFormatter例如:
/** Integer formatter that accepts empty values. */
public class LenientIntegerFormatter extends PropertyFormatter {
public LenientIntegerFormatter(Property propertyDataSource) {
setPropertyDataSource(propertyDataSource);
}
@Override
public Object parse(String formattedValue) throws Exception {
if ("".equals(formattedValue))
return null;
else
return Integer.valueOf(formattedValue);
}
@Override
public String format(Object value) {
if (value == null)
return "";
return ((Integer) value).toString();
}
@Override
public Class<?> getType() {
return String.class;
}
}
它可能看起來有點嚇人,但這是靈活性爲代價的。自定義Bean允許使用表格視圖,表單等,而不做任何重大更改。基本上,這是Vaadin UI背後的數據模型。
![enter image description here](https://i.stack.imgur.com/Iitor.png)
Chapter 9. Binding Components to Data
定製Bean的實例:
public class Bean implements Serializable {
String name;
float value;
public Bean(String name, float newValue) {
this.name = name;
this.value = newValue;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getValue() {
return value;
}
public void setValue(float newValue) {
this.value = newValue;
}
}
只需提供所有需要的見解:
Bean bean = new Bean("Test", value1);
BeanItem<Bean> beanItem = new BeanItem<Bean>(bean);
for(Object propertyId: beanItem.getItemPropertyIds()) {
System.out.println(" Property: '" + propertyId +
"' value: " + beanItem.getItemProperty(propertyId));
}
會打印:
Property: 'name' value: Test
Property: 'value' value: 12.0
這是一個很棒的洞察力,它幫助我完全理解我的問題。我已經使用了一個bean,並且這個bean裏面有了float。 – sokie