Skip to content
forked from javaee/mojarra

Mojarra - Oracle's implementation of the JavaServer Faces specification

License

Notifications You must be signed in to change notification settings

cmunden/mojarra

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mojarra

Oracle's implementation of the JavaServer Faces specification

Minimum Requirements

  • Java 1.8
  • Servlet 3.0 (4.0 recommended)
  • EL 3.0 (3.1 recommended)
  • CDI 1.2 (2.0 recommended)
  • JSTL 1.2
  • JSONP 1.1 (optional, only when <f:websocket> is used)
  • BV 1.1 (optional, only when <f:validateBean> or <f:validateWholeBean> is used; 2.0 recommended)

Servlet 4.0 will enable JSF 2.3 to serve resources via HTTP/2 push. CDI is explicitly required because since JSF 2.3 the javax.faces.bean.* annotations such as @ManagedBean are deprecated, and several implicit EL objects are produced via CDI producers, and <f:websocket> manages the WS sessions and events via CDI.

Installation

Depending on the server used, JSF may already be built-in (full fledged Java EE containers such as WildFly, JBoss EAP, TomEE, Payara, GlassFish, Liberty, etc.), or not (barebones JSP/Servlet containers such as Tomcat, Jetty, etc.). If the server doesn't ship with JSF built-in, then you need to manually install JSF 2.3 along with CDI 1.2+, JSONP 1.1+ and JSTL 1.2+ as those servlet containers usually also don't even ship with those JSF dependencies.

Non-Maven

In case you're manually carrying around JARs:

Maven

In case you're using Maven, you can find below the necessary coordinates:

  • Java EE containers (WildFly, JBoss EAP, TomEE, Payara, GlassFish, Liberty, etc)

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>

    In case you're targeting a Java EE 7.0 runtime, then you should manually upgrade any runtime-provided JSF 2.2 library to JSF 2.3 depending on the server used. In case of WildFly/JBoss EAP, you need to manually package jsf-api.jar and jsf-impl.jar based on javax.faces.jar first. In case of TomEE, just swap the myfaces*.jar files with javax.faces.jar in server's /lib folder. In case of Payara/GlassFish, just swap the javax.faces.jar file in server's /glassfish/modules folder.

  • Servletcontainers (Tomcat, Jetty, etc)

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version><!-- Use latest 2.3.x version. --></version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet-shaded</artifactId>
        <version>3.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency> <!-- Optional, only when <f:websocket> is used. -->
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency> <!-- Optional, only when <f:validateBean> or <f:validateWholeBean> is used. -->
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.1.Final</version>
    </dependency>

    You can check org.glassfish:javax.faces repository to find current latest Mojarra 2.3.x version.

Hello World Example

We assume that you already know how to create an empty Maven WAR Project or Dynamic Web Project in your favourite IDE with a CDI 1.2+ compatible /WEB-INF/beans.xml deployment descriptor file (which can be kept fully empty). Don't forget to add JARs or configure pom.xml if necessary, as instructed in previous chapter.

Controller

Optionally, register the FacesServlet in a Servlet 3.0+ compatible deployment descriptor file /WEB-INF/web.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1"
>
    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

Noted should be that JSF 2.2+ is already "implicitly" registered and mapped on *.jsf, *.faces and /faces/* when running on a Servlet 3.0+ container. This will be overridden altogether when explicitly registering as above. The *.xhtml URL pattern is preferred over above for security and clarity reasons. JSF 2.3+ adds *.xhtml to set of default patterns, hence the FacesServlet registration being optional. But when you don't explicitly map it on *.xhtml, then people can still access JSF pages using *.jsf, *.faces or /faces/* URL patterns. This is not nice for SEO as JSF by design doesn't 301-redirect them to a single mapping.

The JSF deployment descriptor file /WEB-INF/faces-config.xml is fully optional, but if any it must be JSF 2.3 compatible, otherwise JSF 2.3 will run in a fallback modus matching the exact version as declared in <faces-config> root element.

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3"
>
    <!-- Put any faces config here. -->
</faces-config>

Model

Then create a backing bean class as below:

package com.example;

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

@Named
@RequestScoped
public class Hello {

    private String name;
    private String message;

    public void createMessage() {
        message = "Hello, " + name + "!";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

}

Noted should be that in reality in the average Java EE application the above "model" is further breakdown into a JPA entity, an EJB service and a smaller backing bean. The JPA entity and EJB service then basically act as a true "model" and the backing bean becomes a "controller" for that model. This may in first place be confusing to starters, but it all depends on the point of view. See also What components are MVC in JSF MVC framework? and JSF Controller, Service and DAO.

View

Finally create a Facelets file /hello.xhtml as below:

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Hello, World!</title>
    </h:head>
    <h:body>
        <h:form>
            <h:outputLabel for="name" value="Enter your name" required="true" />
            <h:inputText id="name" value="#{hello.name}" />
            <h:message for="name" />
            <br />
            <h:commandButton value="Say hello" action="#{hello.createMessage}">
                <f:ajax execute="@form" render="@form" />
            </h:commandButton>
            <br />
            #{hello.message}
        </h:form>
    </h:body>
</html>

Start the server and open it by http://localhost:8080/contextname/hello.xhtml.

Building

In case you want to checkout this repository and manually build from source yourself (if necessary after editing source code), here are the instructions:

JSF 2.3

  1. Make sure that you have JDK 1.8, Ant and Maven installed.

  2. Checkout branch MOJARRA_2_3X_ROLLING.

  3. Run the following commands from the root directory of the project:

    # under the root dir of project
    cd jsf-tools
    mvn clean install
    cd ../impl
    mvn clean install
  4. The binary is now available as target/javax.faces-2.3.x-SNAPSHOT.jar.

JSF 2.2

  1. Make sure that you have JDK 1.6, Ant and Maven installed.

  2. Checkout branch MOJARRA_2_2X_ROLLING.

  3. Edit build.properties according to your environment. If build.properties does not exist, then create a copy of build.properties.glassfish, build.properties.tomcat or build.properties.weblogic, depending on your target server. Below example assumes GlassFish or Payara:

    cp build.properties.glassfish build.properties

    Only the jsf.build.home property is mandated to be edited in your build.properties. It must represent the absolute path to the root directory of the project.

  4. Run the following command from the root directory of the project:

    # under the root dir of project
    ant main clean main
  5. The binary is now available as jsf-ri/build/mvn/target/javax.faces-2.2.x-SNAPSHOT.jar.

Pull Requests

Pull requests are accepted on following branches:

Note that it's okay to send a PR to the master branch, but these are for JSF.next and not the current 2.3.x version!

Resources

About

Mojarra - Oracle's implementation of the JavaServer Faces specification

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 78.3%
  • HTML 18.2%
  • JavaScript 1.8%
  • CSS 1.0%
  • XSLT 0.5%
  • Shell 0.1%
  • Batchfile 0.1%