如何从另一个 window 触发的托管 bean 中的方法更新(重定向)主 window?

How to update (redirect in) the main window from a method in a managed bean triggered from another window?

我有一个主要的 window (window1),第二个 window (window2) 是由 window.open() 从一个托管 bean 和一个托管 bean(控制器)。

window2 触发控制器中的方法。如果特定条件为真,控制器应关闭 window2 并将页面从 /test/page1.xhtml 更改为 /test/page2.xhtml in window1.

控制器中的方法如下所示:

String result = model.doSomething();

if (result.matches("[0-9]+")) {

    RequestContext.getCurrentInstance().execute("window.close()");

    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();

    try {
        FacesContext.getCurrentInstance().getExternalContext().redirect(request.getContextPath() + "/test/page1.xhtml");
    } catch (IOException e) {
        MessageHelper.showMessage(null, e.getMessage(), FacesMessage.SEVERITY_ERROR, this);
    }
}

我猜问题出在 HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 行。 我不知道如何命令 window1 在关闭 window2.

后立即从控制器更改页面

该解决方案必须在 IE 11 下运行。 我正在使用 Primefaces 5.2 和 JSF 2.0。

这可以简单地使用 JavaScript 完成。只需在 window.close() 之前添加 window.opener.document.location = '{yourlocation}';。所以:

RequestContext.getCurrentInstance().execute(
    "window.opener.document.location = '"+ request.getContextPath() +"/test/page1.xhtml';"+
    "window.close()"
);

另请参阅:

  • When to use window.opener / window.parent / window.top