tomcat - JSF 2.3 无法支持 Tomcat 9 的 WebSocket

标签 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'.

请指教。

最佳答案

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) I

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

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

Mojarra 的安装说明可以在它的 README 中找到。 .

关于tomcat - JSF 2.3 无法支持 Tomcat 9 的 WebSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46955843/

相关文章:

java - Eclipse 中没有创建 XHTML 选项

spring - 文件上传在 Primefaces 中不起作用

wildfly - FacesContext 无法在带有 JSF 2.3 的 Wildfly 14 中注入(inject)(Mojarra,主模块)

jsf - 在 JSF 2.3 中通知用户 session 结束

java - Eclipse 3.6 Tomcat 不会将部署程序集中的 jar 放到它的部署 tmp 目录中

eclipse - Eclipse 上的错误

spring - Tomcat 部署错误

java - 未找到 struts2 Web 应用程序

java - WildFly 仅返回文件名

jsf - JSF 2.3 中 @FacesConverter 和 @FacesValidator 中的 EJB 和托管 bean 注入(inject)