我有一个带有两个布尔字段的User对象:
//User Bean
public class Users {
private Boolean showContactData;
private Boolean showContactDataToContacts;
// Getters + Setters
}
我想在使用Apache Wicket的UI中将其显示为RadioChoice。
HTML部分的片段:
<input type="radio" wicket:id="community_settings"/>
Wicket中带有收音机的Java Form
public class UserForm extends Form<Users> {
public UserForm(String id, Users user) {
super(id, new CompoundPropertyModel<Users>(user));
RadioChoice rChoice = new RadioChoice<Long>("community_settings", choices, renderer);
add(rChoice );
}
}
我的问题是,我现在在用户对象中当然没有属性community_settings。
我只是想将这两个布尔值映射到UI中的单选。
我该如何在Wicket中做到这一点?
谢谢!
塞巴斯蒂安
最佳答案
您需要一个模型来映射数据:
RadioChoice<Long> rChoice = new RadioChoice<Long>("community_settings", new IModel<Long>() {
public Long getObject() {
if (user.getShowContactData() && user.getShowContactDataToContacts()) {
return 1L;
}
// ...
}
public void setObject(Long choice) {
if (choice == 1) {
user.setShowContactData(true);
user.setShowContactDataToContacts(true);
} else if (choice == 2) {
// ...
}
}
public void detach() {
}
}, choices);
顺便说一句,我个人会使用
Enum
,所以根本没有特殊的映射。看来您做的事情太“低级”,因此,检票口模型的工作将使您感到非常麻烦。如果合适,请尝试使用对象而不是原始类型。