在Struts 2中使用WebSocket API [英] Using a WebSocket API with Struts 2

查看:217
本文介绍了在Struts 2中使用WebSocket API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Struts2 web application running on Tomcat 7.0.43 that uses Rest and Convention plugins to map all requests. Struts tries to map all requests by itself.

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

JSR 356 defines server end points using annotations like

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

现在何时broswer尝试连接到 ws:/127.0.0.1:8080 / websocket / chat ,请求失败,因为Struts映射器拦截了请求。

Now when the broswer tries to connect to ws:/127.0.0.1:8080/websocket/chat, the request fails because the Struts mapper intercepts the request.

我可以在XML文件中指定任何内容,以便请求到达正确的位置吗?

Is there anything I can specify in the XML files, such that the request reaches the right place?

编辑:

根据建议,我添加了

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

到我的Struts配置,之后URL / websocket / chat开始达到404错误。

to my Struts configuration, after which the URL /websocket/chat started reaching a 404 error.

后来我了解到我需要配置一个 ServerApplicationConfig 实现。完成后,websocket开始正常工作但我的应用程序的其余部分无法加载给出错误:

Later I learnt that I need to configure a ServerApplicationConfig implementation. After doing that the websocket starts working fine but the rest of my application fails to load giving an error:

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;
    }
}

如何在Harmony中让所有内容协同工作?

How can I get everything to work together in Harmony?

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

Note: I am using Struts Spring Plugin for Dependency Injection of Spring Security.

推荐答案

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

You can configure the Struts filter to exclude some URLs via regex pattern. You should add the constant in the struts.xml

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

这篇关于在Struts 2中使用WebSocket API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆