Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 223
Dima Zelinskyi Avatar asked Sep 14 '14 14:09

Dima Zelinskyi


2 Answers

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.

like image 186
Seitaridis Avatar answered Nov 09 '22 18:11

Seitaridis


According to the javadoc, FacesServlet will be automatically registered if any of the following conditions are true:

  • A faces-config.xml file is found in WEB-INF
  • A faces-config.xml file is found in the META-INF directory of a jar in the application's classpath.
  • A filename ending in .faces-config.xml is found in the META-INF directory of a jar in the application's classpath.
  • The javax.faces.CONFIG_FILES context param is declared in web.xml or web-fragment.xml.
  • The Set of classes passed to the onStartup() method of the ServletContainerInitializer implementation is not empty.

If you are using web.xml only for registering FacesServlet, then it would be optional if any of the above conditions are met.

like image 40
Arun Gupta Avatar answered Nov 09 '22 18:11

Arun Gupta