1

我正在使用将数据绑定到表单binder.addStatusChangeListener( ... )中的部分代码,检查我的 web 应用程序中的返回值。从中得出的MCVE是:

人豆

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在浏览器中更改 's 的内容,我就会得到以下输出:

hasChanges=false
isValid=true

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

4

1 回答 1

3

javadoc

public boolean hasChanges()

检查自上次显式调用 、 或 以来是否有任何绑定字段有未readBean(Object)提交的 removeBean()更改。不成功的写操作不会影响这个值。writeBean(Object)writeBeanIfValid(Object)

请注意,如果您使用setBean(Object)方法,Binder 会在所有验证器通过后尝试提交更改。因此,当使用此方法时,它很少有意义,并且几乎总是返回 false。

每种情况的返回值编译成下表:

╔════════════╦════════════╦═════════╦═════════╦══════════════════╦════════════════════╗
║            ║ 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: 自上次调用 setBean、readBean、writeBean 或 writeBeanIfValid 以来,任何绑定字段的值是否已更改

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

于 2017-04-10T13:47:49.030 回答