我尝试使用 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/