无法识别@WebServlet 注释;init 不运行 [英] @WebServlet annotation not recognized; init doesn't run

查看:21
本文介绍了无法识别@WebServlet 注释;init 不运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习注释.我目前有一个 web 应用程序,当应用程序在 Tomcat 中启动时运行 init().

I'm trying to learn annotations. I currently have a webapp that runs an init() when the app is started in Tomcat.

以下代码有效...

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.company.Main</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

Main.java:

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;

//@WebServlet(name="MainServlet", value="/main.jsp", loadOnStartup=1)
public class Main extends GenericServlet {

    public Main() { }

    @Override
    public void init() {
        System.out.println("Hello!");
    }

    @Override
    public void destroy() {
        System.out.println("Bye!");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { }
}

但是,当我取消注释@WebServlet 注释并注释掉 web.xml 中的 servlet 条目时,init 方法不会运行.

However, when I uncomment the @WebServlet annotation and comment out the servlet entry in web.xml, the init method doesn't run.

我是否遗漏了一些明显的东西?

Am I missing something obvious?

如果这会有所帮助,这是我的 pom.xml:

In case this will help, this is my 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>...</groupId>
    <artifactId>...</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Name</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.6.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

推荐答案

确保你在 servlet 3.0 容器中运行——例如 tomcat 7(tomcat 6 不支持 servlet 3.0)

Make sure you are running in a servlet 3.0 container - tomcat 7, for example (tomcat 6 does not support servlet 3.0)

然后尝试指定 <web-app metadata-complete="false"/> - 默认情况下它应该是 false,但尝试一下.

Then try specifying <web-app metadata-complete="false" /> - it should be false by default, but try it.

这篇关于无法识别@WebServlet 注释;init 不运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆