如何在 PageLoad 上打开 ModalDialog

2022-01-17 00:00:00 modal-dialog java wicket

如何在 PageLoad() 的构造函数中打开一个模式对话框,在没有 AjaxRequestTarget 的情况下使用 Wicket?

How can I open a modal dialog on PageLoad(), in the constructor of the WebPage and without the AjaxRequestTarget, with Wicket?

推荐答案

如果没有 Ajax 请求,我找不到打开它的方法,但是完全可以在页面加载时打开它,只需一个简单的行为:

I couldn't find a way to open it without an Ajax request, but it's perfectly possible to open it when the page is loaded, with a simple behavior:

HomePage.java

HomePage.java

public class HomePage extends WebPage {
    public HomePage(PageParameters pageParameters) {
        super(pageParameters);

        ModalWindow modal = new ModalWindow("modal");
        modal.add(new OpenWindowOnLoadBehavior());
        modal.setPageCreator(new ModalWindow.PageCreator() {
            @Override
            public Page createPage() {
                return new RedirectPage("http://www.google.com");
            }
        });
        add(modal);
    }
}

OpenWindowOnLoadBehavior.java

OpenWindowOnLoadBehavior.java

public class OpenWindowOnLoadBehavior extends AbstractDefaultAjaxBehavior {
    @Override
    protected void respond(AjaxRequestTarget target) {
        ModalWindow window = (ModalWindow) getComponent();
        window.show(target);
    }
    @Override
    public void renderHead(IHeaderResponse response) {
        response.renderOnLoadJavascript(getCallbackScript().toString());
    }
}

主页.html

<html xmlns:wicket="http://wicket.apache.org">
<body>
  <div wicket:id="modal"></div>
</body>
</html>

相关文章