【问题标题】:Spring @WebListener is not availableSpring @WebListener 不可用
【发布时间】:2014-09-04 06:55:31
【问题描述】:

我使用 Spring 4 并想使用 ServletContextListener 。当我在类 eclipse IDE 中添加 @WebListener 注释时显示编译错误。此外,实现还显示编译错误。

public class MyContextListener implements ServletContextListener { // this shows compiler 错误是 ServletContextListener 的基础

我也添加了 spring-web 。 spring 版本是 4.0.3.RELEASE

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>            
    </dependency>

谁能告诉我问题出在哪里。

【问题讨论】:

    标签: java spring spring-mvc jakarta-ee


    【解决方案1】:

    解决 Eclipse 中的编译问题:

    将最新版本的 servlet-api.jar 添加到您的构建路径。

    在你的 pom 中添加 servlet 依赖项:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
      </dependency>
    

    另请参阅:

    Maven compilation failes (but compilation under Eclipse is successful)

    【讨论】:

    • 非常感谢...现在我可以实现监听器了。但是注释不可用@WebListener.. 我该怎么办?
    • 将 servlet-api.jar 添加到您的类路径中。用你的 cmets 更新了我的答案
    • eclipse IDE 给出错误。 Missing artifact javax.servlet:servlet-api:jar:3.0 3.0 版。
    • 谢谢它解决了。但新版本 3.1.0 可以在 mvn repo 中找到。
    • 是的,那么,您绝对可以添加它。从 3.0.1 到 3.1.0 的东西我还没有检查出来。
    【解决方案2】:

    @javax.servlet.annotation.WebListener 注释是servlet-api 不是spring 框架的一部分,如果您使用Tomcat 转到右侧面板中的Project -&gt; Properties -&gt; Project Facets,请选择RunTimes,然后检查您正在使用的Tomcat 服务器实例然后应用。 之后,如果您转到 java build path -&gt; libraries,您将看到 Apache Tomcat vXX 添加到库中。
    或者你可以下载 servlet-api 并将其添加到你的类路径中

    【讨论】:

    • 是的,有了eclipse的配置,现在支持注释了。非常感谢... :)
    【解决方案3】:

    @WebListener 是 javax.servlet 包中的注解,你是否在你的 maven pom 文件中添加了它?

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2021-12-16
      • 2016-02-21
      • 2012-03-23
      • 2015-09-03
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      相关资源
      最近更新 更多