Using a WebSocket API with Struts 2

心已入冬 提交于 2019-12-29 07:18:08

问题


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 defines server end points using annotations like

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

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.

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

EDIT:

As suggested, I added

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

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

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

Here is my class:

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

How can I get everything to work together in Harmony?

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


回答1:


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://.+$"/>


来源:https://stackoverflow.com/questions/20774716/using-a-websocket-api-with-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!