Vaadin 的 Binder#hasChanges() returns 错误 'false'

Vaadin's Binder#hasChanges() returns falsely 'false'

我正在使用 Binding Data to Forms, Checking a return value in my webapp. MCVE 处的代码的 binder.addStatusChangeListener( ... ) 部分派生自:

人豆

public class Person implements Serializable {

    private String name;

    public Person() {}

    public Person( final String name ) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName( final String name ) {
        this.name = name;
    }
}

人物视角

public class PersonView extends VerticalLayout {

    private final Person person = new Person( "Gerold Broser" );
    private final TextField name = new TextField( "Name:" );
    private final BeanValidationBinder<Person> binder =
            new BeanValidationBinder<>( Person.class );
    private final Button button = new Button( "Button" );

    public PersonView() {
        super();

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

        addComponent( name );
        addComponent( button );

        binder.addStatusChangeListener( event -> {
            final boolean hasChanges, isValid;
            out.println( "hasChanges=" + (hasChanges = event.getBinder().hasChanges()) );
            out.println( "isValid=" + (isValid = event.getBinder().isValid()) );
            button.setEnabled( hasChanges && isValid );
        } );
    }
}

只要我在浏览器中更改 TextField 的内容,我就会得到以下输出:

hasChanges=false
isValid=true

这是一个错误还是我遗漏了什么?

来自javadoc

public boolean hasChanges()

Check whether any of the bound fields' have uncommitted changes since last explicit call to readBean(Object), removeBean(), writeBean(Object) or writeBeanIfValid(Object). Unsuccessful writeoperations will not affect this value.

Note that if you use setBean(Object) method, Binder tries to commit changes as soon as all validators have passed. Thus, when using this method with it seldom makes sense and almost always returns false.

Return values for each case are compiled into the following table:

╔════════════╦════════════╦═════════╦═════════╦══════════════════╦════════════════════╗
║            ║ After      ║ After   ║ After   ║ After successful ║ After unsuccessful ║
║            ║ readBean,  ║ valid   ║ invalid ║ writeBean or     ║ writeBean or       ║
║            ║ setBean or ║ user    ║ user    ║ writeBeanIfValid ║ writeBeanIfValid   ║
║            ║ removeBean ║ changes ║ changes ║                  ║                    ║
╠════════════╬════════════╬═════════╬═════════╬══════════════════╬════════════════════╣
║ A bean is  ║            ║         ║         ║                  ║                    ║
║ currently  ║   false    ║  false  ║  true   ║      false       ║     no change      ║
║ bound      ║            ║         ║         ║                  ║                    ║
╟────────────╫────────────╫─────────╫─────────╫──────────────────╫────────────────────╢
║ No bean is ║            ║         ║         ║                  ║                    ║
║ currently  ║   false    ║  true   ║  true   ║      false       ║     no change      ║
║ bound      ║            ║         ║         ║                  ║                    ║
╚════════════╩════════════╩═════════╩═════════╩══════════════════╩════════════════════╝

Returns: whether any bound field's value has changed since last call to setBean, readBean, writeBean or writeBeanIfValid

总之,使用 setBean(myBean) 将触发自动 提交 。因此,如果您想 manually commit changes,请使用 binder.readBean(myBean)& binder.writeBean(myBean).