【问题标题】:Vaadin bind LocalDate and RadioButtonGroupVaadin 绑定 LocalDate 和 RadioButtonGroup
【发布时间】:2018-08-17 05:32:38
【问题描述】:

如何从 Vaadin RadioButtonGroup country 绑定到 Country country DAO 对象?国家是枚举和 DateField(vaadin)LocalDate(DAO)

public class Person {

    @Id
    String id;
    LocalDate dateBorn;
    Country country; 

……

public class PersonFormUI extends GridLayout {

    RadioButtonGroup<Country> country;
    DateField dateBorn; 

........

public enum Country {
    EN, DE, IT }

使用此绑定,所有字段都已绑定并且运行良好,但是如何使用Converter 绑定枚举和日期?

binder.bindInstanceFields(this);
binder.setBean(personDAO);

【问题讨论】:

  • 你需要使用什么样的转换器?还有你用的是什么 Vaadin 版本?
  • 无论什么有助于从 vaadin 字段绑定到 java dao 对象,我都使用最新版本 8.3.0 。 Vaadin 没有我在文档中看到的日期转换器或枚举转换器,或者我必须将枚举和日期解析为字符串?
  • 我相信 8.3.1 是最新的,我也不认为您需要任何转换器或解析字符串,Vaadin 会处理它。您可以在下面我的回答中找到示例。

标签: java spring vaadin bind


【解决方案1】:

如果我可以在直接编写代码之前添加一些建议:

  • 如果你想要一个很好对齐的表格,你可以使用FormLayout
  • 我建议使用ComboBox 而不是RadioButtonGroup 来显示国家/地区,因为它使用的空间更少,并且还允许您通过输入快速找到您想要的内容。但如果你真的想要收音机,请将组合线替换为RadioButtonGroup&lt;Country&gt; countries = new RadioButtonGroup&lt;&gt;("Country", DataProvider.ofItems(Country.values()));

您可以在下面找到基于上述建议和 Vaadin 8.3.1 的绑定示例。我认为不需要任何转换,因为框架会为您处理。

我做的唯一额外的事情是为组合添加一个ItemCaptionGenerator 以显示完整的国家/地区名称,而不是使用枚举名称的默认值(同样的事情可以与RadioButtonGroup 一起使用)。

代码:

public class PersonForm extends FormLayout {

    public PersonForm() {
        // form components
        DateField birthDate = new DateField("Birth date");
        ComboBox<Country> countries = new ComboBox<>("Country", Arrays.asList(Country.values()));

        // use full country name instead of ugly Enum name
        countries.setItemCaptionGenerator(Country::getFullName);

        // do not allow the user to select "nothing"
        countries.setEmptySelectionAllowed(false);

        // binder setup
        Binder<Person> userBinder = new Binder<>();

        // birth date binding
        userBinder.forField(birthDate)
                .asRequired("Please provide a birth date")
                .bind(Person::getDateBorn, Person::setDateBorn);

        // country binding
        userBinder.forField(countries)
                .asRequired("Please select the country of residence")
                .bind(Person::getCountry, Person::setCountry);

        // bind to bean with some existing value (eg, loaded from DB for editing)
        userBinder.setBean(new Person(LocalDate.now(), Country.RO));

        // simulate a save action
        Button saveButton = new Button("Save", event -> Notification.show("Saved new user info: " + userBinder.getBean()));

        // add fields to the UI
        addComponents(birthDate, countries, saveButton);
    }

    // beans
    public class Person {
        private LocalDate dateBorn;
        private Country country;

        public Person(LocalDate dateBorn, Country country) {
            this.dateBorn = dateBorn;
            this.country = country;
        }

        public LocalDate getDateBorn() {
            return dateBorn;
        }

        public void setDateBorn(LocalDate dateBorn) {
            this.dateBorn = dateBorn;
        }

        public Country getCountry() {
            return country;
        }

        public void setCountry(Country country) {
            this.country = country;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "dateBorn=" + dateBorn +
                    ", country=" + country +
                    '}';
        }
    }

    public enum Country {
        RO("Romania"), DE("Deutschland"), IT("Italy");

        private String fullName;

        Country(String fullName) {
            this.fullName = fullName;
        }

        public String getFullName() {
            return fullName;
        }
    }
}

结果:


你可以更进一步,根据binder status禁用按钮(也许你想使用相同的表单来添加没有初始值的新人):

// disable saving until all required data is available
userBinder.addStatusChangeListener(event -> saveButton.setEnabled(userBinder.isValid()));`

结果:

【讨论】:

  • 我仍然是 vaadin 的新手,但是非常感谢你做了一个快速的示例,非常感谢你是上帝 :-D 我想如果我使用 binder.bindInstanceFields(this);对于相同的字段,它将自动绑定,我必须使用 binder.bind thnaks 验证其他一些属性
  • 我不想使用 FormLayout 来解决我之前问过的问题:-D 我使用带有简单开关 x、y 组件的 GL 添加到我想要的位置,就像我的演示 petmiksoft.herokuapp.com
  • @petmik,不客气。如果您使用快速代码示例编辑您的other question,我们可能也可以找出您对FormLayout 的问题,但就像我说的那样,如果没有办法重现问题,很难弄清楚是什么正在发生。
  • 好吧,这很头疼,我用导航解决了这个问题,我几天就学会了它是如何工作的
  • 顺便说一句,枚举字符串常量的好提示我有其他枚举,但有变音符号,我看到单选按钮里面有 setItemCaptionGenerator 我做了棘手的傻瓜循环 MAN("muž"), WOMAN("žena"), CHILD( "dítě");
猜你喜欢
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
相关资源
最近更新 更多