【问题标题】:Wicket: Lazy loading DropDownChoiceWicket:延迟加载 DropDownChoice
【发布时间】:2025-12-07 02:45:01
【问题描述】:

我的 web 应用程序上的一个 DropDownChoice 列表需要很长时间才能创建,因为通过 LDAP 连接和 SQL 连接的某些操作获取选项。正因为如此,整个页面的加载时间远远超过了几秒钟——我想说的太多了。

所以我想要实现的是使用(最适合我)Wicket 的内置 Ajax 功能来延迟加载这个下拉列表,但是我有一些问题。

我知道如何制作常规的 DropDownChoice 列表,这个简单的示例对我来说非常有用 - link

我也知道如何从 wicket-examples - link (Source Code -> LazyLoadingPage.html/LazyLoadingPage.java) 制作延迟加载的段落

但是把它放在一起会抛出异常并导致 Wicket 的内部错误。

这是我尝试的方法:

在 HTML 中:

<select wicket:id="lazy"></select>

在 Java 中:

private String selected = "abc";
(...)
add(new AjaxLazyLoadPanel("lazy") {

            @Override
            public Component getLazyLoadComponent(String id) {
                //simulating long time for simple list
                try {
                    Thread.sleep(5000);
                }
                catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                return new DropDownChoice<String>(
                        id, new PropertyModel<String>(this,"selected"),
                        Arrays.asList("abc","def"));

            }
        });
    }

我从 Wicket 收到内部错误,并在日志中显示:

ERROR Unexpected error occurred
Component [content] (path = [0:lazy:content]) must be applied to a tag of type [select], not:  '<div wicket:id="content">' (line 0, column 0)
 MarkupStream: [markup = jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%207.0/webapps/devservices/WEB-INF/lib/wicket-extensions-1.5.7.jar!/org/apache/wicket/extensions/ajax/markup/html/AjaxLazyLoadPanel.html

,索引 = 0,当前 = ''

和堆栈跟踪。

非常感谢一些帮助,我做错了什么,或者一些更好的代码示例。

【问题讨论】:

  • 尝试将 DDC 放入面板(或片段)并在 getLazyLoadComponent() 中构造/返回它
  • @bert 你成就了我的一天!完美运行 - 非常感谢!!!
  • 还可以查看 select2 wicket 实现,请参阅 github.com/ivaynberg/wicket-select2

标签: java ajax web-applications wicket


【解决方案1】:

感谢bert,我将完整的解决方案放在这里,以防将来有人使用它。

我们需要创建自己的面板,因为 AjaxLazyLoadPanel 只能将一个面板更改为另一个。

MyPanel.html 示例:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
<body>
<wicket:panel>
    <select wicket:id="project"></select>
</wicket:panel>
</body>
</html>

和 MyPanel.java :

public class MyPanel extends Panel {
    private String selected = <what you want>;
    private List<String> projectList <what you want>;
    public MyPanel(String id) {
        super(id);
        add(new DropDownChoice<String>(
           "project", new PropertyModel<String>(this, "selected"), projectsList));
    }
}

在您的主页 html 上只需添加以下内容:

<span wicket:id="lazy2"></span>

在主页java文件中:

add(new AjaxLazyLoadPanel("lazy") {
    @Override
    public Component getLazyLoadComponent(String id) {
        return new MyPanel(id);
    }
});

希望它也能帮助其他人:-)

【讨论】: