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

查看:28
本文介绍了在 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")

现在当浏览器尝试连接到 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天全站免登陆