2

Web アプリケーションで選択ボックスを含むフォームを作成しています。以下で説明する方法で、DropDownChoice + ChoiceRenderer の組み合わせを使用しています。正常に動作しますが、1 つには、デフォルト値が事前に選択されていません。

私はかなり長い間これに苦労してきました。インターネット上で、うまくいくと言われているいくつかの例を見つけましたが、私にはうまくいきませんでした。

私のコードベースは次のようになります (わかりやすくするために、無関係な部分は省略または簡略化されています)。

事業体

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;
    }
}

ダオ

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();
        }
     };
}

Form コンポーネントの CompundPropertyModel としても、DropDownChoice コンポーネントのモデルとしても、デフォルトの MultivalueType インスタンスを (冗長に) 設定していることに注意してください。私がインターネットで見つけたものに基づいて、ページがロードされたときに選択ボックスが対応する値を事前に選択するにはこれで十分なはずですが、機能しません。

Form コンポーネントのプロパティ変数 (DAO から取得) に有効なデータが実際に含まれていることを確認しました。

また、すべてが正常に機能することに注意してください。ただし、事前選択の場合は、フォームの onSubmit メソッドで正しく入力された propertySettings 変数を取得しています。

4

1 に答える 1

5

私はついに問題を見つけることができました。

フォームを表示すると、Wicket が次の警告をログに記録することに気付きました。

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

そのエラー メッセージに基づいて、デフォルト オプションの事前選択が機能するためには、ビジネス エンティティが equals メソッドをオーバーライドする必要があることがわかりました (対応する JIRA チケットを参照してください)。少なくとも、私が使用している Wicket バージョン 1.5.4 では。

これが同じ問題で立ち往生する他の人に役立つことを願っています!

于 2012-09-01T08:47:58.680 に答える