June 27, 2017

Getting Started with JSF 2.0 (EE 6)


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.


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





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;

// 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"

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

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



No comments: