![](/img/trans.png)
[英]DropDownChoices is returning null as value of selected item from drop down list
[英]Initialization of required DropDownChoices and null value
這是我的問題,我有一個必需的DropDownChoices。 但是在我的選擇列表中,我有一個項目包含一個空/空代碼,我認為這是一個有效的答案。 我可以使它接受(下面的解決方案),但是我不能用正確的值對其進行初始化。
這是我的想法,我有這個對象: Item { String code; String label}
Item { String code; String label}
和下面的代碼:
List<Item> choices = new ArrayList<Item>();
choices.add(new Item("ok","labelOk"));
choices.add(new Item("","labelEmpty"));
ChoiceRenderer<Item> renderer = new ChoiceRenderer<Item>("label", "code");
DropDownChoice<Item> s = new DropDownChoice<Item>("id", Model.of(new Item("","labelEmpty")), choices, renderer);
s.setRequired(true);
僅此而已,第二選擇就不被接受。 因此,我創建了一個自定義渲染器,將null值替換為none:
public class ItemChoiceRenderer extends ChoiceRenderer<Item> {
public TListItemChoiceRenderer() {super("label", "code");}
@Override
public String getIdValue(Item object, int index) {
if (object != null && StringUtils.isNullOrEmpty(object.getCode())) {
return "none";
}
return super.getIdValue(object, index);
}
}
這樣,我可以選擇第二個選擇。 但是DropDownChoices不會在正確的值上初始化,因此在開始時選擇“選擇一個”。
看起來該組件未使用渲染器選擇其字段,但是我看不到原因以及如何糾正此問題。
沒關系,我發現了我的問題,DropDownChoices的模型未初始化。
我讓這個問題出現,因為它顯示了我的問題的解決方案,它可能會讓某些人感興趣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.