Wicket:更新 DataView 中的 DropDownChoice

如何解决Wicket:更新 DataView 中的 DropDownChoice

DownloadPage

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

因此,在版本 3 的 id 160 上单击“下载”应下载 file160ver3.txt,而在版本 2 的 id 159 上应下载 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",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",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()
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?