ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-在Struts 2中使用WebSocket API

2019-10-30 03:00:30  阅读:227  来源: 互联网

标签:websocket struts2 tomcat7 spring java


我有一个运行在Tomcat 7.0.43上的Struts2 Web应用程序,该应用程序使用Rest和Convention插件来映射所有请求. Struts尝试自己映射所有请求.

JSR 356使用如下注释来定义服务器端点

@ServerEndpoint(value = "/websocket/chat")

现在,当浏览器尝试连接到ws:/127.0.0.1:8080 / websocket / chat时,该请求将失败,因为Struts映射器会拦截该请求.

我可以在XML文件中指定什么内容,以使请求到达正确的位置?

编辑:

按照建议,我添加了

<constant name="struts.action.excludePattern" value="/websocket.*?" />

到我的Struts配置中,然后URL / websocket / chat开始出现404错误.

后来我了解到我需要配置ServerApplicationConfig实现.这样做之后,websocket开始正常运行,但是我的应用程序的其余部分无法加载,并给出了错误:

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

这是我的课:

public class Socket implements ServerApplicationConfig {

    @Override
    public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned) {

        Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();

        return result;
    }

    @Override
    public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {

        Set<Class<?>> results = new HashSet<Class<?>>();
        for (Class<?> clazz : scanned) {
            results.add(clazz);
        }
        return results;
    }
}

我如何才能在“和谐”中一起工作?

注意:我正在使用Struts Spring插件进行Spring Security的依赖注入.

解决方法:

您可以将Struts过滤器配置为通过正则表达式模式排除某些URL.您应该在struts.xml中添加常量

<constant name="struts.action.excludePattern" value="^ws://.+$"/>

标签:websocket,struts2,tomcat7,spring,java
来源: https://codeday.me/bug/20191030/1964838.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有