Wicket:更新 DataView 中的 DropDownChoice

Wicket: Update DropDownChoice in DataView

我在 Wicket 中制作了一个下载页面。如您所见,它是一个 DataView,您可以在其中下载文件,具体取决于 id 列和 DropDownChoice 'version'.

因此,在 ID 160 上点击 'Download' 版本 3 应该下载 file160ver3.txt,而在 ID 159 上点击版本 2 应该下载 file159ver2.txt。不幸的是,更新 DropDownChoice 并未反映在模型中。所以点击下载按钮总是下载相同版本的文件。由于我在我的 DropDownChoice 中默认使用版本 2,所以它总是下载这个版本。

这是我的代码:

DropDownChoice<Integer> choice = new DropDownChoice<>("version", new Model<Integer>(2), List.of(1, 2, 3));
choice.add(new AjaxEventBehavior("change") {
    @Override
    protected void onEvent(AjaxRequestTarget target) {
        target.add();
        System.out.println(choice.getModelObject());    // doesn't change
    }
});
item.add(choice);

// The value of choice.getModelObject() doesn't change
DownloadLink download = new DownloadLink("download", getFile(p.getId(), choice.getModelObject()));
download.setOutputMarkupId(true);
item.add(download);

我错过了什么?如何更新 DropDownChoice?

更新及解决方案(根据Svens建议修改):

        choice.add(new AjaxFormComponentUpdatingBehavior("change") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println(choice.getModelObject());
            }
        });
        item.add(choice);

        DownloadLink download = new DownloadLink("download", () -> {
            return getFile(p.getId(), choice.getModelObject());
        });

        // ...

private File getFile(int id, DropDownChoice<Integer> choice) throws FileNotFoundException, IOException {
    Integer version = choice.getModelObject();

谢谢。

... 这是完整的代码(下面的 Java 和 HTML):

public DownloadPage(PageParameters params) {
    List<PrefKey> prefKeys = db.getPrefKeys();
    DataView<PrefKey> dataView = getDataView(prefKeys);
    Form<Void> form = new Form<>("form");
    add(form);
    form.add(dataView);
}

private DataView<PrefKey> getDataView(List<PrefKey> prefKeys) {
    IDataProvider<PrefKey> provider = new ListDataProvider<>(prefKeys);
    DataView<PrefKey> dataView = new DataView<>("dbAsDataView", provider, 10) {
        private static final long serialVersionUID = 12345L;

        @Override
        protected void populateItem(Item<PrefKey> item) {
            PrefKey p = item.getModelObject();
            item.add(new Label("tdId", p.getId()));
            item.add(new Label("tdKey", p.getKey()));
            try {
                DropDownChoice<Integer> choice = new DropDownChoice<>("version", new Model<Integer>(2), List.of(1, 2, 3));
                choice.add(new AjaxEventBehavior("change") {
                    @Override
                    protected void onEvent(AjaxRequestTarget target) {
                        target.add();
                        System.out.println(choice.getModelObject());    // doesn't change
                    }
                });
                item.add(choice);

                DownloadLink download;
                // The value of choice.getModelObject() doesn't change
                download = new DownloadLink("download", getFile(p.getId(), choice.getModelObject()));
                download.setOutputMarkupId(true);
                item.add(download);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    return dataView;
}

    <h1>Wicket Download</h1>
    <form wicket:id="form" action="">
        <table id="tblDataView" class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Key</th>
                    <th>Version</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody>
                <tr wicket:id="dbAsDataView">
                    <td wicket:id="tdId"></td>
                    <td wicket:id="tdKey"></td>
                    <td><select wicket:id="version"></select></td>
                    <td><input type="button" wicket:id="download" value="Download"></input></td>
                </tr>
            </tbody>
        </table>
    </form>

您必须使用 AjaxFormComponentUpdatingBehavior 将新选择的项目传输到 Java 组件(及其模型):

choice.add(new AjaxFormComponentUpdatingBehavior("change") {
  @Override
  protected void onUpdate(AjaxRequestTarget target) {
  }
});

https://ci.apache.org/projects/wicket/guide/8.x/single.html#_ajaxformcomponentupdatingbehavior

然后您的下载链接也已动态调整为当前选择:

download = new DownloadLink("download", () -> {
    return getFile(p.getId(), choice.getModelObject()
});