Wicket-DropDownChoice + ChoiceRenderer-预选不起作用 [英] Wicket - DropDownChoice + ChoiceRenderer - preselecting does not work

查看:39
本文介绍了Wicket-DropDownChoice + ChoiceRenderer-预选不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Web应用程序中创建带有选择框的表单.我以下面描述的方式使用DropDownChoice + ChoiceRenderer组合.它工作正常,但有一点-in不预先选择默认值.

I am creating a form with a select-box in my web application. I am using the DropDownChoice + ChoiceRenderer combination in the below described way. It works fine, but for one thing - in does not preselect default values.

我已经为此苦苦挣扎很长时间了.我在互联网上找到了几个可以用的示例,但对我却不起作用.

I have been struggling with this for quite a long time. I have found several examples on the Internet which were said to work but they didn't work for me.

我的代码库如下所示(不相关的部分已被省略或简化以提高清晰度):

My codebase looks like this (unrelated parts have been omitted or simplified to improve clarity):

业务实体

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组件的模型.根据我在Internet上的发现,选择框应足以在页面加载时预选择相应的值,但它不起作用.

Note that I'm setting the default MultivalueType instance (redundantly) both as a CompundPropertyModel of the Form component and directly as a model of the DropDownChoice component. Based on what I have found on the Internet, this should be enough for the select-box to preselect the corresponding value when the page loads, but it does not work.

我已经验证了Form组件(从DAO获得)中的属性变量确实包含有效数据.

I have verified that the property variable in the Form component (obtained from the DAO) really contains valid data.

还请注意,除了预选择之外,其他所有方法都工作正常-我在表单的onSubmit方法中正确填充了propertySettings变量.

Also note that everything works fine but for the preselecting - I'm getting correctly populated propertySettings variable in the onSubmit method of the form.

推荐答案

我终于设法找到了问题.

I've finally managed to find the problem.

我已经意识到,当我显示表单时,Wicket会记录以下警告:

I've realized that when I display the form, Wicket logs the following warning:

Model returned object ... which is not available in the list of selected objects.

基于该错误消息,我发现,为了使默认选项有效,业务实体必须重写equals方法(请参见

Based on that error message I've found out that in order for the preselecting of default options to work, the Business Entities must override the equals method (see the corresponding JIRA ticket). At least in Wicket version 1.5.4, which I'm using.

希望这可以帮助陷入同样问题的其他人!

Hope this helps others who will get stuck with the same problem!

这篇关于Wicket-DropDownChoice + ChoiceRenderer-预选不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆