【问题标题】:Vaadin ComboBox: Convert between String and POJOVaadin ComboBox:在字符串和 POJO 之间转换
【发布时间】: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


【解决方案1】:

如果你需要 ComboBox 给你一个值 Language 你可以扩展 ComboBox 类:

class LanguageComboBox extends ComboBox {
    public LanguageComboBox(String caption, Collection<Language> languages) {
        super(caption, getShortcuts(languages));
    }

    @Override
    public Language getValue() {
       return new Language((String) super.getValue());
    }

    private static List<String> getShortcuts(Collection<Language> languages) {
       // extract shortcuts using java 1.8 
       return languages.stream()
            .map(language -> language.getShortcut())
            .collect(Collectors.toList());
    }
}

然后您可以使用BeanItem&lt;Customer&gt; 轻松地将其添加到FieldGroup

    ComboBox comboBox = new LanguageComboBox("Language", LANGUAGES);
    fieldGroup.bind(comboBox,"language");

提交后,您的客户将获得适当的语言价值。

在我看来,您不应该在视图上使用实体 - 正如您所提到的,您无法更改 Customer 对象,并且可能这个类包含您的视图所需的更多字段。我宁愿为此视图创建单独的 POJO 类 - 这为您提供了更大的灵活性,并且不会出现您报告的此类问题。当然,最后您需要将视图对象转换为实体或更多实体,但在大多数情况下,承担该成本是值得的。

【讨论】:

    【解决方案2】:

    您可以将快捷方式值作为 THE 值添加到 ComboBox 并定义可选标题:

        cmbx.addContainerProperty("caption", String.class, "");
        cmbx.setItemCaptionPropertyId("caption");
    
        cmbx.addItem(languageEN.getLanguageShortCut()).getItemProperty("caption").setValue(languageEN.getLanguageLong());
        cmbx.addItem(languageFR.getLanguageShortCut()).getItemProperty("caption").setValue(languageFR.getLanguageLong());
    

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 2021-09-13
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      相关资源
      最近更新 更多