【发布时间】:2014-05-08 08:07:47
【问题描述】:
使用FieldGroup,我有一个表单绑定Customer 的所有属性,使用BeanItem<Customer> 并在FormLayout 中显示它们。
我的Customer 类是这样的:
@Entity
@Table(name="customer")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String description;
(... other fields ...)
private String language;
(... getters and setters ...)
}
属性language 是该语言的快捷方式。比如"de"、"en"、"fr"等等。
此外,还有一个名为 Language 的 POJO 类(代码简化)
public class Language {
protected String shortCut;
public Language(String shortCut)
{
this.shortCut = shortCut;
}
public String getShortcut()
{
return this.shortCut;
}
}
...和一个静态的实例列表(代码简化):
public static List<Language> LANGUAGES = new ArrayList<>(Arrays.asList(new Language[]{
new Language("de"),
new Language("fr"),
new Language("en")
}));
除了表单布局中的客户数据,我还添加了另一个包含上述语言列表的组合框。
我想要实现的是,在提交表单时,会采用组合框中选择的语言并将其快捷方式写入BeanItem。伪代码:
// Assuming we have a combobox and our customer bean, both created before and elsewhere:
ComboBox cmbLanguage;
BeanItem<Customer> customer;
// Then, the language chosen by the user is:
Language lang = (Language)cmbLanguage.getValue();
// So this should actually happen upon saving the form:
customer.getItemProperty("language").setValue(lang.getShortcut());
我知道可以使用字段组上的提交处理程序手动更改客户 bean 对象。但我想避免这种情况。
有没有办法“自动”在组合框、表单或 bean 项上实现转换器等?
查看 N. Frankel's presentation "Vaadin 7 FieldGroup & Converter"(第 18 页到第 21 页)后,我尝试使用 Converter 的自定义实例。但这显然行不通,因为绑定属性不能同时是语言集/列表和客户语言。
如何做到这一点?
注意:由于互操作性,我无法更改Customer,我需要将结构与语言类一起使用。
【问题讨论】:
-
你能澄清你的问题吗?从描述来看,如果您只是想在实体中绑定组合框和字段,则不清楚您要做什么以及为什么需要召集人。
-
改变了我的问题。我希望现在更清楚了。
标签: combobox converter vaadin7