我需要在@WebListener中构建一些jsf urls。
我想我可以用像这样的代码片段
final FacesContext currentInstance = FacesContext.getCurrentInstance();
final String actionURL = currentInstance.getApplication().getViewHandler()
.getActionURL(currentInstance, viewId);因为.getCurrentInstance()的javadoc断言它可以“.在应用程序初始化或关闭期间调用”,但是它不能工作,因为它返回null。
我想念什么吗?给定viewId,还有其他方法来构建url吗?
谢谢
发布于 2015-11-20 10:41:43
FacesContext.getCurrentInstance()方法确实返回了一个有效的facesContext实例,该实例位于linstener (在我的示例中是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()。
https://stackoverflow.com/questions/33800296
复制相似问题