如何在Wicket DropDownChoice中获取选定的值?

时间:2014-11-15 13:45:04

标签: java wicket wicket-1.6 dropdownchoice

在wicket形式中,我有一个DropDownChoice,我想在其中选择一个值。我有:

private final List<DimSpecific> specificList;
private DimSpecific specificPtr = null;
...
specificList = roles.getSpecificList();
specificPtr = new DimSpecific();
DropDownChoice specific = new DropDownChoice("specific", new Model<>(specificPtr), specificList, new ChoiceRenderer<DimSpecific>("code", "id"));
Form form = new Form("frm_0_07"){
    @Override
    protected void onSubmit() {
        String specificSelected = specificPtr.getCode();
    }
}

变量specificSelected等于null。我如何获得选定的价值?

2 个答案:

答案 0 :(得分:0)

您是否已将DropdownChoice添加到表单?

您传入的模型对象将被更新 - 而不是对象本身。在您的情况下,它是一个未分配的变量(新模型&lt;&gt;(specificPtr)),因此您无法从中读取。

试试这个:

private final List<DimSpecific> specificList;
private DimSpecific specificPtr = new DimSpecific(); // or init to some default value
private IModel<DimSpecific> dropdownModel = new PropertyModel<DimSpecific>(this, "specficicPtr");
...
specificList = roles.getSpecificList();
DropDownChoice specific = new DropDownChoice("specific", dropdownModel , specificList, new ChoiceRenderer<DimSpecific>("code", "id"));
Form form = new Form("frm_0_07"){
    @Override
    protected void onSubmit() {
        String specificSelected = dropdownModel.getObject();
    }
}

这也会使specificPtr等于所选的值 - 原因是PropertyModel告诉dropdownchoice在提交的所选下拉列表中setObject()的位置 - 到specificPtr。

答案 1 :(得分:0)

试试这个:

List<DimSpecific> list = getList(); //not importent
Model<DimSpecific> dimSpecificModel = new Model<DimSpecific>();
ChoiceRenderer<DimSpecific> choiceRenderer = new ChoiceRenderer<>("code", "id");
DropDownChoice<DimSpecific> choice = new DropDownChoice<DimSpecific>("specific", dimSpecificModel, list, choiceRenderer);

Button button = new Button("button", Model.of("")){
    @Override
    public void onSubmit() {
        System.out.println(dimSpecificModel.getObject());
    }
};