我正在我的网络应用程序中创建一个带有选择框的表单。我按照下面描述的方式使用 DropDownChoice + ChoiceRenderer 组合。它工作正常,但有一件事 - in 不会预先选择默认值。
我已经为此苦苦挣扎了很长一段时间。我在互联网上找到了几个据说有效的示例,但它们对我不起作用。
我的代码库如下所示(已省略或简化不相关的部分以提高清晰度):
商业实体
public class MultivalueType
{
private Long id;
private String label;
private String description;
public MultivalueType(Long id, String label, String description)
{
this.id = id
this.label = label;
this.description = description;
}
public Long getId()
{
return id;
}
public String getLabel()
{
return label;
}
public String getDescription()
{
return description;
}
}
public class PropertySettings
{
private Long id;
private String property;
private MultivalueType multivalueType;
public PropertySettings(Long id, String property, MultivalueType multivalueType)
{
this.id = id;
this.property = property;
this.multivalueType = multivalueType;
}
public MultivalueType getMultivalueType()
{
return multivalueType;
}
}
DAO
public class PropertySettingsDao
{
...
@Override
public PropertySettings load(Long id)
{
String query = " ... query ... ";
Object[] params = { id };
return getJdbcTemplate().queryForObject(query, params, getRowMapper());
}
...
}
表单类
PropertySettings property = propertySettingsDao.load(propertyId);
IModel<PropertySettings> formModel = new CompoundPropertyModel<PropertySettings>(property);
Form<PropertySettings> form = new Form<PropertySettings>("editPropertyForm", formModel)
{
@Override
protected void onSubmit()
{
PropertySettings propertySettings = this.getModelObject();
...
}
};
form.add(createEnumSelectbox(multivalueTypeDao, new PropertyModel<MultivalueType>(property, "multivalueType"), "multivalueType", true));
add(form);
创建选择框
protected DropDownChoice<MultivalueType> createEnumSelectbox(DaoForEntityWithSurrogateKey<MultivalueType> dao, IModel<MultivalueType> model, String componentName, boolean required)
{
// create the model
IModel<List<MultivalueType>> choices = createModelForListView(dao);
// prepare the select-box renderer
ChoiceRenderer<MultivalueType> renderer = new ChoiceRenderer<MultivalueType>("label", "id");
// create the select-box component
DropDownChoice<MultivalueType> selectBox = new DropDownChoice<MultivalueType>
(
componentName,
model,
choices,
renderer
);
// mark the select-box as a required form field
selectBox.setRequired(required);
return selectBox;
}
protected IModel<List<MultivalueType>> createModelForListView(final DaoForEntityWithSurrogateKey<MultivalueType> dao)
{
return new LoadableDetachableModel<List<MultivalueType>>()
{
@Override
protected List<BO> load()
{
return dao.loadAll();
}
};
}
请注意,我将默认的 MultivalueType 实例(冗余地)设置为 Form 组件的 CompundPropertyModel 并直接设置为 DropDownChoice 组件的模型。根据我在互联网上找到的信息,这应该足以让选择框在页面加载时预先选择相应的值,但它不起作用。
我已验证 Form 组件中的属性变量(从 DAO 获取)确实包含有效数据。
另请注意,一切正常,但对于预选 - 我在表单的 onSubmit 方法中正确填充了 propertySettings 变量。
最佳答案
我终于找到了问题所在。
我发现当我显示表单时,Wicket 会记录以下警告:
Model returned object ... which is not available in the list of selected objects.
根据该错误消息,我发现为了使默认选项的预选起作用,业务实体必须重写 equals 方法(请参阅 corresponding JIRA ticket )。至少在我正在使用的 Wicket 版本 1.5.4 中是这样。
希望这可以帮助其他遇到同样问题的人!
关于java - Wicket - DropDownChoice + ChoiceRenderer - 预选不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12226118/