June 27, 2017

Postback Lifecycle in JSF 2.0 (EE 6)

"The request-response lifecycle handles two kinds of requests: initial requests and postbacks. An initial request occurs when a user makes a request for a page for the first time. A postback request occurs when a user submits the form contained on a page that was previously loaded into the browser as a result of executing an initial request."

Reference: Oracle Java EE 6 Tutorial The Lifecycle of a JavaServer Faces Application

Getting Started with JSF 2.0 (EE 6)

Introduction

JSF 2.0 is the standard Web Framework that ships with Java EE 6. Here I will build a simple JSF web app to get you started with the build stones.

Maven

With starting with Java EE 6, there is a ONE dependency for all Java EE, namely javax:javaee-api:[6.0|7.0]. A basic pom.xml file for Java EE 6 is.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>se.magnuskkarlsson.examples</groupId>
    <artifactId>example-jsf20</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

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

    <build>
        <finalName>${project.artifactId}</finalName>
    </build>
</project>

Deployment Descriptors

Starting with EE 6 the web.xml is no longer compulsory, so here we will skip it.

A new feature in JSF 2.0, is that you do not need to declare navigation in the faces-config.xml, but you need to declare. That is the same thing with CDI. To enable CDI you need to have beans.xml. So we have to deployment descriptors.

  • WEB-INF/faces-config.xml
  • WEB-INF/beans.xml

Another feature in JSF 2.0, is that you can use ordinary CDI annotation instead of JSF specific. This makes one thing less to remember.

So instead of @javax.faces.bean.ManagedBean we can use @javax.inject.Named.

And instead of @javax.faces.bean.RequestScoped we can use @javax.enterprise.context.RequestScoped.

So a simple request scope baking bean for JSF is.

package se.magnuskkarlsson.example.jsf;

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

@Named
@RequestScoped
// Shorthand for @Named and @RequestScoped is @javax.enterprise.inject.Model
public class UserBean {

    private String name;

    public String getMessage() {
        return (name != null) ? "Hello " + name : null;
    }

    public String getName() {
        return name;
    }

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

}

And finally our simple hello JSF page. Notice the commandButton and action, it points to JSF page hello, i.e. to the page itself.

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Example JSF 2.0 Hello</title>
</h:head>
<h:body>
    <h:form>
        <h:outputLabel value="What is your name?" for="name" />
        <h:inputText id="name" value="${userBean.name}" />
        <h:commandButton value="Submit" action="hello" />
    </h:form>
    <p>
        <h:outputText value="${userBean.message}"
            rendered="${not empty userBean.message}" />
    </p>
</h:body>
</html>

Now build and deploy it to e.g. JBoss EAP 6, the app is accessible from either

http://localhost:8080/example-jsf20-1.0.0-SNAPSHOT/hello.jsf

http://localhost:8080/example-jsf20-1.0.0-SNAPSHOT/faces/hello.xhtml

June 17, 2017

JSF 2.0 Facelets Tag Libraries

Tag Library

"JSP technology is considered to be a deprecated presentation technology for JavaServer Faces. Facelets is a part of the JavaServer Faces specification and also the preferred presentation technology for building JavaServer Faces technology-based applications."

Tag Library URI Prefix Example Contents
JavaServer Faces HTML Tag Library http://java.sun.com/jsf/html h: h:head
h:body
h:outputText
h:inputText
JavaServer Faces component tags for all UIComponent objects
JavaServer Faces Core Tag Library http://java.sun.com/jsf/core f: f:actionListener
f:attribute
Tags for JavaServer Faces custom actions that are independent of any particular render kit
JavaServer Faces Facelets Tag Library http://java.sun.com/jsf/facelets ui: ui:component
ui:insert
Tags for templating
JSTL Core Tag Library http://java.sun.com/jsp/jstl/core c: c:forEach
c:catch
JSTL 1.2 Core Tags
JSTL Functions Tag Library http://java.sun.com/jsp/jstl/functions fn: fn:toUpperCase
fn:toLowerCase
JSTL 1.2 Functions Tags

Reference: Oracle Java EE 6 Tutorial: What Is Facelets?

Facelet Template

<?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://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:c="http://java.sun.com/jsp/jstl/core"
 xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <h:outputStylesheet library="css" name="default.css" />
 <title>Facelets Template</title>
</h:head>
<h:body>

 <h1>Hello</h1>

</h:body>
</html>