. Vaadin Framework, Vaadin, :
, .
Binder#bindInstanceFields()
:
, . . HasValue<String>
Integer
( age). IllegalStateException
, bindInstanceFields(Object)
.
[...]: bindInstanceFields(Object)
.
[ .]
, AFAIU, :
private final TextField siblingsCount = new TextField( "№ of Siblings" );
...
binder.forField( siblingsCount )
.withNullRepresentation( "" )
.withConverter(
new StringToIntegerConverter( Integer.valueOf( 0 ), "integers only" ) )
.bind( Child::getSiblingsCount, Child::setSiblingsCount );
binder.bindInstanceFields( this );
:
java.lang.IllegalStateException: Property type 'java.lang.Integer' doesn't match the field type 'java.lang.String'. Binding should be configured manually using converter.
...
at com.vaadin.data.Binder.bindInstanceFields(Binder.java:2135)
...
? , ? , " ". , , , bindInstanceFields()
, .
, Binder#bindInstanceFields()
, .
. Integer Vaadin Framework issue # 8858 Binder.bindInstanceFields() .
, @cfrick :
/** Used for workaround for Vaadin issue
* 'Binder.bindInstanceFields() overwrites existing bindings'
* https://github.com/vaadin/framework/issues/8858
*/
private final Map<String, Component> manualBoundComponents = new HashMap<>();
...
// Commented here and declared local below for workaround for Vaadin issue
//private final TextField siblingsCount = new TextField( "№ of Siblings" );
...
public ChildView() {
...
// Workaround for Vaadin issue
// Declared local here to prevent processing by Binder
final TextField siblingsCount = new TextField( "№ of Siblings" );
manualBoundComponents.put( "siblingsCount", siblingsCount );
binder.forField( siblingsCount )
.withNullRepresentation( "" )
.withConverter( new StringToIntegerConverter( Integer.valueOf( 0 ), "integers only" ) )
.bind( Child::getSiblingsCount, Child::setSiblingsCount );
binder.bindInstanceFields( this );
...
// Workaround for Vaadin issue
addComponent( manualBoundComponents.get( "siblingsCount" ) );
//addComponent( siblingsCount );
...
}
UPDATE
# 8998 bindInstanceFields , .
Vaadin 8.1.0 alpha 4 pre-release (, , ).
...
, , Binder::bindInstanceFields
(Integer) . , Binder::bindInstanceFields
, " ".
. Vaadin 8.1.0 alpha 3. yearOfBirth
. binder.bindInstanceFields
@PropertyId
name
. .
- , ? , .
package com.example.vaadin.ex_formatinteger;
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;
@Theme ( "mytheme" )
public class MyUI extends UI {
Person person;
final TextField honorific = new TextField ( "Honorific:" );
final TextField name = new TextField ( "Full name:" );
final TextField yearOfBirthField = new TextField ( "Year of Birth:" );
final Label spillTheBeanLabel = new Label ( );
@Override
protected void init ( VaadinRequest vaadinRequest ) {
this.person = new Person ( "Ms.", "Margaret Hamilton", Integer.valueOf ( 1936 ) );
Button button = new Button ( "Spill" );
button.addClickListener ( ( Button.ClickEvent e ) -> {
spillTheBeanLabel.setValue ( person.toString ( ) );
} );
Binder < Person > binder = new Binder <> ( Person.class );
binder.forField ( this.yearOfBirthField )
.withNullRepresentation ( "" )
.withConverter ( new StringToIntegerConverter ( Integer.valueOf ( 0 ), "integers only" ) )
.bind ( Person:: getYearOfBirth, Person:: setYearOfBirth );
binder.bindInstanceFields ( this );
binder.setBean ( person );
setContent ( new VerticalLayout ( honorific, name, yearOfBirthField, button, spillTheBeanLabel ) );
}
@WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
@VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}
Person
.
package com.example.vaadin.ex_formatinteger;
import java.time.LocalDate;
import java.time.ZoneId;
public class Person {
private String honorific ;
private String name;
private Integer yearOfBirth;
public Person ( String honorificArg , String nameArg , Integer yearOfBirthArg ) {
this.honorific = honorificArg;
this.name = nameArg;
this.yearOfBirth = yearOfBirthArg;
}
public String getHonorific ( ) {
return honorific;
}
public void setHonorific ( String honorific ) {
this.honorific = honorific;
}
public String getName ( ) {
return name;
}
public void setName ( String nameArg ) {
this.name = nameArg;
}
public Integer getYearOfBirth ( ) {
return yearOfBirth;
}
public void setYearOfBirth ( Integer yearOfBirth ) {
this.yearOfBirth = yearOfBirth;
}
public Integer getAge ( ) {
int age = ( LocalDate.now ( ZoneId.systemDefault ( ) )
.getYear ( ) - this.yearOfBirth );
return age;
}
@Override
public String toString ( ) {
return "Person{ " +
"honorific='" + this.getHonorific () + '\'' +
", name='" + this.getName () +
", yearOfBirth=" + this.yearOfBirth +
", age=" + this.getAge () +
" }";
}
}