【发布时间】:2014-06-07 10:27:54
【问题描述】:
已解决:我使用了错误的命名空间,请参阅本文后的说明。
当尝试运行嵌入式 tomcat 并导航到 jsf 页面时,jsf 标记只是呈现到页面中并且它们不会被执行。
编辑: 我将此 maven 项目添加到
https://github.com/traugott/embedded-tomcat-example-jsf-dont-work.git
简单地克隆它
git clone https://github.com/traugott/embedded-tomcat-example-jsf-dont-work.git
创建tomcat嵌入式服务器的文件是:
package launch;
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
public class Main {
public static void main(String[] args) throws Exception {
String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.setBaseDir(".");
tomcat.getHost().setAppBase(".");
tomcat.setSilent(false);
// Add AprLifecycleListener
StandardServer server = (StandardServer) tomcat.getServer();
AprLifecycleListener listener = new AprLifecycleListener();
server.addLifecycleListener(listener);
Context ctx = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
Tomcat.addServlet(ctx, "jsf_servlet", "javax.faces.webapp.FacesServlet");
ctx.addServletMapping("*.xhtml", "jsf_servlet");
tomcat.start();
tomcat.getServer().await();
}
}
web.xml(在src/main/webapp/WEB-INF下)是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
<display-name>Test</display-name>
<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>index.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
index.xhtml 是(在 src/main/webapp/ 下):
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
</h:head>
<h:body>
<h:outputText value="Hallo JSF-Tomcat-Embedded-Welt"/>
</h:body>
</html>
最后是 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>com.heroku.sample</groupId>
<artifactId>embeddedTomcatSample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>embeddedTomcatSample Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<tomcat.version>7.0.34</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.13</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.13</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<finalName>embeddedTomcatSample</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>launch.Main</mainClass>
<name>webapp</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我不知道问题出在哪里,但我想我错过了什么。
有人可以帮忙吗?
【问题讨论】:
-
好吧,我从来没有那样运行过 Tomcat,但是拥有一个 web.xml 文件来声明 servlet 并且还必须在 Java 代码中再次声明 servlet 的目标是什么?有没有办法告诉嵌入的tomcat web.xml 文件在哪里?我猜你没有正确配置它,也没有加载 JSF 库,正如症状所暗示的那样。
-
你说得对,两次声明 servlet 没有意义。我调试到 FacesServlet 并且遇到断点命中,所以至少 Tomcat 调用了 Jsf servlet。我是否必须为标签库添加一些 Maven 依赖项?
-
您不必这样做。因此,如果 Servlet 被命中,那么直接在客户端获取 JSF 标记听起来很奇怪。尝试声明一个 JSF PhaseListener 以测试是否执行了整个循环:stackoverflow.com/a/8389346/1199132
-
我为所有阶段实现了一个 PhaseListener,将其推送到 git 存储库并得到以下语句: LogPhaseListener.beforePhase:RESTORE_VIEW 1 LogPhaseListener.afterPhase:RESTORE_VIEW 1 LogPhaseListener.beforePhase:RENDER_RESPONSE 6 LogPhaseListener.afterPhase: RENDER_RESPONSE 6
-
看来 servlet 正在正确呈现页面,所以我不知道问题可能出在哪里..
标签: java jsf-2 embedded-tomcat-7