Search code examples
jsfweb.xmljsf-2.2java-ee-7

How to run a JSF 2.2 page in Java EE 7 environment without web.xml?


What is wrong with my very very simple web app: web app successfully deployed to app server but hello bean did not inject to index.xhtml page (page shows just Hello from Facelets: #{hello.value})?

(this is first time when I am working with JSF, so maybe this question is very easy, and also I used good article http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java-ee-app-without-any.html )

I have the next structure of war archive:

mywebapp
|
 - WEB_INF
  |
   - classes
     |
      - Hello.class
 - index.html

Hello.java has:

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class Hello {

    private String value;

    public String getValue() {
        return "Hello JSF";
    }

    public void setValue(String value) {
        this.value = value;
    }
}

and also my index.xhtml

<?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">
    <h:head>
        <title>My Facelet Page Title</title>
    </h:head>
    <h:body>
        Hello from Facelets: #{hello.value}
    </h:body>
</html>

For building project I used pom.xml:

....
<packaging>war</packaging>
<name>Simple web app</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Solution

  • Taken from JavaServerFaces 2.0, The Complete Reference:

    An entry in the Web application’s web.xml file enables the Faces Controller servlet when a certain URL pattern is specified, such as /faces/. When running JSF 2.0 on a Servlet 3.0 container, such as Sun’s Glassfish v3, the web.xml is optional. If no web.xml is found, the Faces Controller servlet is automatically mapped to the most popular URL patterns: /faces/, .jsf, and .faces.

    So you should try with something like this:

    localhost:8080/mywebapp/faces/index.xhtml.