2017-02-23 99 views
0

嗨所以我是Vaadin的新手,嘗試創建窗體並綁定到該POJO對象。Vaadin與SpringBoot

...Some declaration 

Binder<User> binder = new Binder<>(User.class); 

@Autowired 
public FormUser(UserRepository userRepository, AuthorityRepository authorityRepository){ 
    this.userRepository = userRepository; 
    this.authorityRepository = authorityRepository; 

    authorities = new ListSelect<>("Authorities", authorityRepository.findAll()); 
    authorities.setItemCaptionGenerator(Authority::getAuthority); 

    //Set items 
    username.setIcon(FontAwesome.USER); 
    password.setIcon(FontAwesome.USER_SECRET); 
    saveButton.addClickListener(e -> { 
     userRepository.save(user); 
    }); 

    setSpacing(true); 
    addComponents(username, password, authorities, saveButton); 
    binder.bindInstanceFields(this); 
} 

當嘗試訪問視圖包含FormUser得到這個錯誤:

java.lang.IllegalStateException: Property type 'java.util.Collection' doesn't match the field type 'java.util.Set< dev.gva.model.Authority >'. Binding should be configured manually using converter.

管理局

public class Authority{ 
    private Long id; 
    private String authority; 

    getter/setters.. 
} 

用戶

public class User{ 
    private Long id; 
    private Collection<Authority> authorities; 

    other fields, getters/setters... 
} 

如何寫這個轉換器?由於

+0

您試過了'authorities.setConverter(new Converter ,Collection >(){/*...*/})'? –

+0

@ A.Meier我會盡快嘗試,謝謝你的建議 – GVArt

回答

0

而是添加樣板代碼的轉換,你應該使用設置列表User -class內的authorities -attribute。這樣做的一個優點是在authorities中不允許有重複。 設置之間的區別列表列表是有序的,可以通過索引訪問。你決定你需要什麼,但是設置可能就夠了。