在 Tomcat 中部署 JSF2 应用程序时看不到欢迎页面



我想使用mvn tomcat:run在tomcat上部署我的JSF2应用程序。我之前使用mvn clean install命令正确地编译了它。Tomcat对我说:INFO: Starting Coyote HTTP/1.1 on http-8080

我想知道我是否正确设置了我的欢迎页面。这是我的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_2_5.xsd"
    version="2.5">
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>
    <welcome-file-list>
        <welcome-file>home.xhtml</welcome-file>
    </welcome-file-list>

</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>org.bogus</groupId>
    <artifactId>jsf2_tutorial</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>jsf2_tutorial Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.0-b03</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.0-b03</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <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>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

只是我要粘贴最后一位,home.xhtml以确保它也是正确的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Hello JSF 2!</title>
    </h:head>
    <h:body>
       #{welcome.message}
    </h:body>
</html>

当我导航到欢迎页面时,错误是:

HTTP status 500
In the url I notice I had a typo. Now I enter the correct URL but the problem is:
    type Exception report
    message
    description The server encountered an internal error () that prevented it from fulfilling this request.
    exception
javax.servlet.ServletException: Class javax.faces.webapp.FacesServlet is not a Servlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:662)
root cause
java.lang.ClassCastException: javax.faces.webapp.FacesServlet cannot be cast to javax.servlet.Servlet
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Thread.java:662)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

我通过从pom.xml

中删除servlet-api依赖来消除异常。
 <dependency>  <groupId>javax.servlet</groupId> 
 <artifactId>servlet-api</artifactId>  <version>2.5</version> 
 </dependency>

页面现在加载,但没有显示消息。这个应用程序的java部分是这样的:

package org.bogus;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "welcome", eager = true)
public class WelcomeBean {
    public WelcomeBean() {
        System.out.println("WelcomeBean instantiated");
    }
    public String getMessage() {
        return "I'm alive!";
    }
}

这个例子几乎与oracle的官方JSF教程相同:http://docs.oracle.com/javaee/6/tutorial/doc/gjaam.html

我包括了<load-on-startup>1</load-on-startup>,但我仍然没有在控制台或页面中看到消息。也许构型还是有问题。任何想法?

我刚刚在WEB-INF下包含了faces-config.xml文件,但仍然不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is not required if you don't need any extra configuration. -->
<faces-config version="2.1"
   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-facesconfig_2_1.xsd">
   <!-- Write your navigation rules here. -->
   <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>en</supported-locale>
        </locale-config>
   </application>
</faces-config>

你基本上有两个问题:

  1. JSF2要求至少Servlet 2.5(兼容Tomcat 6),但是您的web.xml被声明为符合Servlet 2.3(兼容Tomcat 4)。您应该将web.xml声明为与servlet容器兼容的最高版本。如果它是Tomcat 6,它是一个Servlet 2.5容器,则将其声明为Servlet 2.5。

    <?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_2_5.xsd"
        version="2.5">
        <!-- Config here. -->
    </web-app>
    

    (是的,没有doctype!)


  2. Mojarra 2.1.0的
  3. @ManagedBean不能在Tomcat和Jetty上工作,因为在注释扫描代码中意外插入了特定于glassfish的代码。另见1937年版。您需要至少升级到Mojarra 2.1.1(或者只升级到当前可用的2.1.17)。另一种方法是在faces-config.xml中手动注册受管bean,但这显然很笨拙。


与具体问题无关,@ManagedBean(eager=true)只作用于@ApplicationScoped

相关内容

  • 没有找到相关文章

最新更新