当Binder
在 Vaadin 8 中将 a 与具有只读计算属性的 bean 一起使用时,该属性的值从另一个属性派生,当驱动属性的值发生更改时,如何让 aTextField
自动更新派生计算结果的显示?
在以下示例中,当用户更改“出生年份”字段时,如何让“年龄”字段更新其计算?
Vaadin 8 的完整工作示例。
package com.example.val;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Binder;
import com.vaadin.data.converter.StringToIntegerConverter;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
import javax.servlet.annotation.WebServlet;
import java.time.LocalDate;
import java.time.ZoneId;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of a html page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme ( "mytheme" )
public class MyUI extends UI {
Person person;
Binder < Person > binder;
@Override
protected void init ( VaadinRequest vaadinRequest ) {
// Data model
this.person = new Person ( "Jean-Luc", 1955 );
// Widgets
final TextField nameField = new TextField ( "Type the person’s name here:" );
final TextField yearOfBirthField = new TextField ( "Type the year of birth here:" );
final TextField ageField = new TextField ( "Approximate age:" );
ageField.setReadOnly ( true );
final Label beanToString = new Label ( );
// Binder
this.binder = new Binder <> ( );
binder.forField ( nameField )
.bind ( Person:: getName, Person:: setName );
binder.forField ( yearOfBirthField )
.withConverter ( new StringToIntegerConverter ( "Input must be Integer" ) )
.bind ( Person:: getYearOfBirth, Person:: setYearOfBirth );
binder.forField ( ageField )
.withConverter ( new StringToIntegerConverter ( "" ) )
.bind ( Person:: getAge, null );
binder.setBean ( this.person );
final Button button = new Button ( "Save" );
button.addClickListener ( event -> {
if ( binder.validate ( ).isOk ( ) ) {
// With `setBear`, the Person object is always up-to-date as long as there are no validation errors.
// MyBackend.updatePersonInDatabase(person);
beanToString.setValue ( this.person.toString ( ) );
} else { // Else bean flunks validation.
beanToString.setValue ( "The Person bean has invalid state." );
}
} );
this.setContent ( new VerticalLayout ( nameField, yearOfBirthField, ageField, button, beanToString ) );
}
@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}
class Person {
private Integer yearOfBirth;
private String name;
public Person ( String name_, Integer yearOfBirth_ ) {
this.name = name_;
this.yearOfBirth = yearOfBirth_;
}
public String getName ( ) {
return name;
}
public void setName ( String name ) {
this.name = name;
}
public Integer getYearOfBirth ( ) {
return yearOfBirth;
}
public void setYearOfBirth ( Integer yearOfBirth ) {
this.yearOfBirth = yearOfBirth;
}
// Read-only property 'age', calculated rather than stored.
public Integer getAge ( ) {
LocalDate today = LocalDate.now ( ZoneId.systemDefault ( ) );
Integer years = ( today.getYear ( ) - this.yearOfBirth );
return years;
}
@Override
public String toString ( ) {
return "Person{ " +
"yearOfBirth=" + yearOfBirth +
", age='" + this.getAge ( ) + "'" +
", name='" + name + "'" +
" }";
}
}