JSF 2.3无法通过Tomcat 9支持WebSocket

时间:2017-10-26 13:35:40

标签: tomcat jsf-2 jsf-2.3

我尝试使用JSF 2.3.3(Glassfish实现)测试新的WebSocket功能。我使用Tomcat 9.0.1作为Web服务器,并遵循本指南(https://javaserverfaces.github.io/whats-new-in-jsf23.html

我创建了一个托管bean:

@Named
@ApplicationScoped
public class Controller {

    @Inject @Push
    private PushContext cityChannel;

    public void send() {
        cityChannel.send("test");
    }
}

更新了index.xhtml:

<f:websocket channel="cityChannel"
            onmessage="function(message){alert(message)}" />

更新了web.xml:

<context-param>
    <param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
    <param-value>true</param-value>
</context-param>

不幸的是,Tomcat无法加载带有错误的应用程序:

SEVERE [main] com.sun.faces.config.ConfigureListener.contextInitialized Critical error during deployment:
 javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/javax.faces.push/{channel}] : existing endpoint was [class com.sun.faces.push.WebsocketEndpoint] and new endpoint is [class com.sun.faces.push.WebsocketEndpoint]
        at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:169)

如果我删除了上下文参数javax.faces.ENABLE_WEBSOCKET_ENDPOINT,那么当我点击index.xhtml时,JSF运行时会引发错误:

Caused by: java.lang.IllegalStateException: f:websocket endpoint is not enabled. You need to set web.xml context param 'javax.faces.ENABLE_WEBSOCKET_ENDPOINT' with value 'true'.

请建议。

1 个答案:

答案 0 :(得分:2)

  

javax.websocket.DeploymentException:多个端点可能未部署到同一路径[/javax.faces.push/{channel}]:现有端点为[class com.sun.faces.push.WebsocketEndpoint]和新端点是[类com.sun.faces.push.WebsocketEndpoint]           at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:169)   我

当运行时类路径被多个Mojarra实现污染时,会发生这种情况。例如,一个来自jsf-impl.jar文件而另一个来自javax.faces.jar文件。或者甚至可能是多个不同的版本。

您需要确保运行时类路径中只有一个版本的库。这不适用于Mojarra,而是适用于每个基于Java的库。

Mojarra的安装说明可在README

中找到