0

我有一个在 tomcat 7.0.29 中运行的 JSF 2.0 Web 应用程序,并尝试使用 Servlet 3.0 注释,但 servlet 不起作用,因为我看不到它的 init() 方法中写入的日志。

我已经阅读了很多相同问题的答案,但仍然没有成功。

这是我的文件的外观:

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app metadata-complete="false"
    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">
  <display-name>GestionCongesFeki</display-name>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>


<!--   <servlet> -->
<!--     <servlet-name>AjaxRelaisServlet</servlet-name> -->
<!--     <servlet-class>utils.AjaxRelaisServlet</servlet-class> -->
<!--     <load-on-startup>2</load-on-startup> -->
<!-- </servlet> -->
<!-- <servlet-mapping> -->
<!--     <servlet-name>AjaxRelaisServlet</servlet-name> -->
<!--     <url-pattern>/AjaxRelaisServlet/*</url-pattern> -->
<!-- </servlet-mapping> -->

</web-app>

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>esprit.pfe2013</groupId>
  <artifactId>GestionCongesFeki</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>GestionCongesFeki Maven Webapp</name>
  <url>http://maven.apache.org</url>

<repositories>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>       
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Maven Plugin Repository</name>
            <url>http://repo1.maven.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>

<!--   <repositories> -->
<!-- <repository> -->
<!--            <id>central</id> -->
<!--            <name>Central Repository</name> -->
<!--            <url>http://repo.maven.apache.org/maven2</url> -->
<!--            <layout>default</layout> -->
<!--            <snapshots> -->
<!--                <enabled>false</enabled> -->
<!--            </snapshots> -->
<!--        </repository> -->


<!--    </repositories> -->

    <dependencies>

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



        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.4.1</version>
        </dependency>


        <!-- JSF 2 -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.11</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>




        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.13</version>
        </dependency>


        <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
</dependency>






    </dependencies>
    <build>
    <finalName>GestionCongesFeki</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>  

</project>

和小服务程序:

package utils;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AjaxRelaisServlet
 */
@WebServlet("/AjaxRelaisServlet") 
public class AjaxRelaisServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxRelaisServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        System.out.println("This is AjaxRelaisServlet initialisaton !!!");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

实际上,当我在 web.xml(注释代码)中声明它时,servlet 工作正常,但当我使用注释时却没有。顺便说一句,我想知道当这个问题得到解决时我是否可以摆脱 web.xml,但我不知道如果没有它,JSF servlet 将如何声明。

感谢帮助!

4

1 回答 1

6

loadOnStartup=1将该属性添加到您的@WebServlet注释中,以使 servlet 在应用程序启动时加载。此属性类似于<load-on-startup/>您将在web.xml中找到的元素。

    @WebServlet(name="AjaxRelaisServlet",loadOnStartup=1,urlPatterns={"/AjaxRelaisServlet"})

当你有一把锤子时,每一个问题看起来都像钉子。

在完全替换web.xml的问题上,您应该意识到基于注释的模型目前无法实现的一些事情。例如,仅使用注释(AFAIK)无法实现过滤器排序。此外,您应该意识到具体的配置文档是自记录的。您系统的维护者可以查看 web.xml 并了解您的应用程序中发生了什么,配置明智。将其与仅注释架构进行对比。弄清楚系统所有部分的布线将是一项艰巨的任务。

于 2013-03-29T16:41:51.290 回答