【问题标题】:Using a WebSocket API with Struts 2在 Struts 2 中使用 WebSocket API
【发布时间】:2017-05-07 07:21:03
【问题描述】:

我有一个在 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;
    }
}

我怎样才能让所有的东西在 Harmony 中协同工作?

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

【问题讨论】:

  • 分区问题将帮助您更快地获得答案。正如您发现的那样,您可以防止 struts 在当前没有 struts2/websocket 集成之后尝试对某些 url 采取行动。这两种技术都是面向前端的(服务器上的最前端层)并且几乎没有交互。但是将两者结合成一个问题会减少听众。让事情协调工作的答案是在服务层开发业务逻辑,s2 和 ws 都会使用。
  • 我也面临这个问题@Manu你有什么解决办法吗?如果你有,请告诉我。提前致谢。

标签: java spring struts2 websocket tomcat7


【解决方案1】:

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

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

【讨论】:

  • 添加了常量,但现在 ws 请求导致 404 错误。知道如何让它到达聊天类/阅读注释吗?
  • 编写一个服务器端点并将其部署到您的应用程序中,我没有使用 websockets 但您可以在tutorial 中找到示例。
猜你喜欢
  • 2011-03-22
  • 2012-07-30
  • 2016-06-23
  • 1970-01-01
  • 2014-02-16
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多