将不会调用ServletContextListener

时间:2018-03-15 22:03:14

标签: java tomcat7

实际上这是一个传统的问题,但我发现的所有解决方案都不起作用,但我想它必须是一个愚蠢的解决方案:所以我有一个动态的Web项目,其中包含一个ContextListener,它覆盖了两个方法contextInitialized和contextDestroyed并使用@WebListener进行注释。

我的pom.xml看起来像:

<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.mygroup</groupId>
<artifactId>mytestproject</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
 <name>mytestproject Maven Webapp</name>

<dependencies>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>
</dependencies>
<build>
<finalName>mytestproject</finalName>
 <pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.2</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>exec-war-only</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
  </plugins>
</pluginManagement>

<plugins>
  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
  </plugin>

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
        </plugin>
    </plugins>
  </build>
</project>

我的类ContextListener.java看起来像:

package de.mygroup.mypackage;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("The application started");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("The application stopped");
    }
}

我的web.xml看起来像:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

我可以创建jar文件并启动它。当我调用localhost:8080时,我只能看到一个空的白页,而且我没有在命令promt中看到ContextListenerClass中定义的输出。有什么我忘了吗?似乎他注释@WebListener不起作用。

提前致谢 DD

0 个答案:

没有答案
相关问题