在Struts 2中使用WebSocket API

Man*_*anu 3 java spring struts2 websocket tomcat7

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

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

@ServerEndpoint(value = "/websocket/chat")
Run Code Online (Sandbox Code Playgroud)

现在,当浏览器尝试连接时ws:/127.0.0.1:8080/websocket/chat,由于Struts映射器拦截了该请求,因此请求失败。

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

编辑:

按照建议,我添加了

<constant name="struts.action.excludePattern" value="/websocket.*?" />
Run Code Online (Sandbox Code Playgroud)

到我的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
Run Code Online (Sandbox Code Playgroud)

这是我的课:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Rom*_*n C 5

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

<constant name="struts.action.excludePattern" value="^ws://.+$"/>
Run Code Online (Sandbox Code Playgroud)