WebSocket连接不使用Strut2

发布于 2025-01-20 07:11:13 字数 2281 浏览 0 评论 0原文

我举了例子 https://github.com/slankka/websocket-with-struts2

Error : WebSocket connection  to 'ws://localhost:8080/websocket' failed: 

Web.xml

<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts2 Web Application</display-name>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.action.excludePattern" value="/websocket*,^ws://.$"/>
    <package name="controllers" namespace="/"  extends="struts-default">
        <action name="homePage" class="io.github.slankka.controllers.HomeAction" method="home">
           <result name="SUCCESS">/home.jsp</result>
        </action>
    </package>
</struts>

ChatServer.java

package io.github.slankka.controllers;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@SuppressWarnings("unused")
@ServerEndpoint("/websocket")
public class ChatServer {
...
}

URL:http://localhost:8080/web_socket_struts2_test/chat.jsp

Error : WebSocket connection to 'ws://localhost:8080/websocket' failed: 

在此处输入图像描述

也尝试了 stackoverflow 上的许多解决方案,但还没有任何效果。

I have taken example from
https://github.com/slankka/websocket-with-struts2

Error : WebSocket connection  to 'ws://localhost:8080/websocket' failed: 

Web.xml

<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts2 Web Application</display-name>
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.action.excludePattern" value="/websocket*,^ws://.
quot;/>
    <package name="controllers" namespace="/"  extends="struts-default">
        <action name="homePage" class="io.github.slankka.controllers.HomeAction" method="home">
           <result name="SUCCESS">/home.jsp</result>
        </action>
    </package>
</struts>

ChatServer.java

package io.github.slankka.controllers;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

@SuppressWarnings("unused")
@ServerEndpoint("/websocket")
public class ChatServer {
...
}

URL : http://localhost:8080/web_socket_struts2_test/chat.jsp

Error : WebSocket connection to 'ws://localhost:8080/websocket' failed: 

enter image description here

Tried many solutions from stackoverflow as well but nothing work yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舟遥客 2025-01-27 07:11:13

您缺少URL中的Web应用程序上下文路径。

ws://localhost:8080/[CONTEXT_NAME]/websocket

有时,上下文名称和项目名称相同。

例子:

ws://localhost:8080/web_socket_struts2_test/websocket

You are missing web application context path in the URL.

ws://localhost:8080/[CONTEXT_NAME]/websocket

Sometimes context name and project name are the same.

EXAMPLE:

ws://localhost:8080/web_socket_struts2_test/websocket
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文