1

我需要在@WebListener.

我以为我可以使用这样的代码片段

    final FacesContext currentInstance = FacesContext.getCurrentInstance();
    final String actionURL = currentInstance.getApplication().getViewHandler()
            .getActionURL(currentInstance, viewId);

因为.getCurrentInstance()断言的 javadoc 可以“在应用程序初始化或关闭期间调用 [...]”,但它不起作用,因为它返回 null。

我错过了什么吗?给定 viewId 的任何其他方式来构建 url?

谢谢

4

1 回答 1

1

FacesContext.getCurrentInstance()方法确实facesContext在 linsener(com.sun.faces.config.ConfigureListener在我的情况下)初始化和 FacesServlet 的第一次运行之间返回一个有效实例,其中facesContext释放了侦听器的设置。我的问题是我让 Wildfly 添加侦听器,它是在我之后添加的。强制将其加载到web-fragment.xml/web.xml

<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
    <listener-class>com.company.project.somepackages.Listener</listener-class>
</listener>

让我的听众初始化上下文。

然而,上面的代码不起作用,因为 viewHandler 在尝试解析 contextPath 时使用的externalContext.getRequestContextPath()方法显然返回null,而不是externalContext.getApplicationContextPath()可以返回正确值的方法。

于 2015-11-20T10:41:43.283 回答