0

SOLVED: I used the wrong namespace, see explanation after this post.

When trying to run embedded tomcat and navigating to a jsf page, the jsf tags are simply rendered into the page and they doesn't get executed.

EDIT: I added this maven project to

https://github.com/traugott/embedded-tomcat-example-jsf-dont-work.git

simply clone it with

git clone https://github.com/traugott/embedded-tomcat-example-jsf-dont-work.git

The file to create the tomcat embedded server is:

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();
  }
}

The web.xml (under src/main/webapp/WEB-INF) is:

<?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>

The index.xhtml is (under 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>

and, at last, the 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>

I don't know where the problem is, but i guess i missed something.

Can anybody help ?

user2706510
  • 141
  • 1
  • 9
  • Well, I have never run Tomcat that way, but what is the goal of having a web.xml file where you declare the servlet and also having to declare the servlet again in the Java code? Isn't there a way to tell the embeded tomcat where the web.xml file is? I guess you're not configuring it properly and also it's not loading the JSF libraries, as the symptoms suggest. – Aritz Jun 08 '14 at 11:49
  • You're right, it didn't make sense to declare the servlet twice. I debugged into the FacesServlet and i got a breakpoint hit, so at least Tomcat calls the Jsf servlet. Do i have to add some maven dependencies for the Tag libraries ? – user2706510 Jun 08 '14 at 19:27
  • You don't have to. So, if the Servlet is being hit, it sounds strange to get the JSF tags directly at client side. Try to declare a JSF PhaseListener in order to test that the whole cycle is performed: http://stackoverflow.com/a/8389346/1199132 – Aritz Jun 09 '14 at 06:17
  • I implemented a PhaseListener for all Phases, pushed it into the git repository and got the following statements: LogPhaseListener.beforePhase:RESTORE_VIEW 1 LogPhaseListener.afterPhase:RESTORE_VIEW 1 LogPhaseListener.beforePhase:RENDER_RESPONSE 6 LogPhaseListener.afterPhase:RENDER_RESPONSE 6 – user2706510 Jun 09 '14 at 08:57
  • It seems the servlet is properly rendering the page, so I have no idea where the problem could be.. – Aritz Jun 09 '14 at 09:20

1 Answers1

0

I go it. I've used the wrong namespace. In the question above the namespace is still wrong (it is the namespace for jsf 2.2). I uploaded a index.xhtml with the correct namespace to github.

For the versions given in the pom.xml-File, the namespace for the jsf html tags have to be: http://java.sun.com/jsf/html

user2706510
  • 141
  • 1
  • 9