我正在我的Web应用程序中创建一个带有选择框的表单.我以下面描述的方式使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
DAO
public class PropertySettingsDao
{
...
@Override
public PropertySettings load(Long id)
{
String query = " ... query ... ";
Object[] params = { id };
return getJdbcTemplate().queryForObject(query, params, getRowMapper());
}
...
}
Run Code Online (Sandbox Code Playgroud)
表格类
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);
Run Code Online (Sandbox Code Playgroud)
创建选择框
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();
}
};
}
Run Code Online (Sandbox Code Playgroud)
请注意,我将默认的MultivalueType实例(冗余地)设置为Form组件的CompundPropertyModel,并直接设置为DropDownChoice组件的模型.根据我在互联网上找到的内容,这应该足以让选择框在页面加载时预先选择相应的值,但它不起作用.
我已经验证了Form组件中的属性变量(从DAO获得)确实包含有效数据.
另请注意,一切正常但是对于预选 - 我在表单的onSubmit方法中正确填充了propertySettings变量.
我终于设法找到了问题.
我意识到当我显示表单时,Wicket会记录以下警告:
Model returned object ... which is not available in the list of selected objects.
Run Code Online (Sandbox Code Playgroud)
基于该错误信息,我发现了,为了为默认选项的预选工作,业务实体必须覆盖equals方法(参见相应的JIRA票).至少在我正在使用的Wicket 1.5.4版本中.
希望这能帮助那些陷入同样问题的人!
| 归档时间: |
|
| 查看次数: |
3874 次 |
| 最近记录: |