[英]Vaadin with SpringBoot
嗨,我是Vaadin的新手,嘗試創建表單並綁定到該POJO對象。
...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:屬性類型'java.util.Collection'與字段類型'java.util.Set <dev.gva.model.Authority>'不匹配。 綁定應使用轉換器手動配置。
權限 :
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...
}
如何寫這個轉換器? 謝謝
而不是添加轉換的樣板代碼,您應該使用Set或List作為User
類中的authorities
屬性。 這樣做的好處還在於, authorities
不允許重復。 Set和List之間的區別在於List是有序的,可以由索引訪問。 您決定需要什么,但是Set可能就足夠了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.