A short treatise on
JavaServer Faces (JSF)


Russell Bateman
last update: 23 Feburary 2010

JavaServer (JSF) is a user-interface (UI) component based the Java web application framework. JSF is serverbased, that is the JSF UI components and their state are represented on the server. A JSF application run in a standard web container, e.g.: Tomcat, GlassFish, Jetty, etc. JSF defines a standard life-cycle for the UI components.

Tools you need

  1. JDK; a simple Java Runtime Environment (JRE) will not do
  2. Eclipse
  3. Tomcat
  4. Libraries
    1. Standard Tag Library for JavaServer Pages (JSTL); you need both the JSTL API and the JSTL Implementation JARs.

Categorization

JSF is part of the Java EE standard, formerly known as J2EE. Sun Microsystems provides a reference implementation but there are also alternatives such as Apache MyFaces.

A JSF application consists of web pages containing JSF UI components. This application requires two configuration files, web.xml and faces-config.xml".

web.xml is identical in use and composition to this file in a JavaServer Pages (JSP) application. JSF builds on JSF as a logical step beyond.

faces-config.xml defines:

  1. managed beans, data elements of the JSF application (both "managed beans" and "backing beans"). These are a Java class that is created dynamically as the JSF application runs. They can be defined in the scope for which the bean is valid, at the level of "session," "request," "application" or "none."
  2.  
  3. Actual navigation between web pages.
  4.  
  5. data validators, used to check the validity of UI input
  6.  
  7. data converters, used to translate between UI and model
  8.  

Managed beans

A managed bean is a type of JavaBean or reusable software component, that is created and initialized with dependency injection. Dependency injection is a technique for supplying external values upon which a component or bean is dependent, usually for its initialization.

Java EE 6: a parenthesis...

Beginning in Java EE 6, a managed bean is a Java class that...

Example

Consider the class Person, ...

	package com.etretatlogiciels.jsf.treatise;

	public class Person
	{
	  String firstName;
	  .
	  .
	  .
	}

Managed beans are "plain, old Java objects" (POJOs) declared in faces-config.xml. For example, you can define a Java object Person. Once you define the object in faces-config.xml you use the attributes of Person in your UI components by binding the field, say, firstName of an object of this class to a JSF input field.

JSF uses the Java Unified Expression Language, used also by JSP, to bind UI components to object attributes or methods. This "language" consists of the XML tags you will come to recognized in the JSP/JSF code.

A managed bean is specified in faces-config.xml thus:

	<managed-bean>
	  <managed-bean-name>                Person                        </managed-bean-name>
	  <managed-bean-class>  com.etretatlogiciels.jsf.treatise.Person   </managed-bean-class>
	  <managed-bean-scope>               session                       </managed-bean-scope>
	</managed-bean>
<managed-bean-scope> session|request|application|none </managed-bean-scope>

It is the framework within the Tomcat (etc.) container that ensures the communication of values between HTML (user/presentation) and the component (POJO).

Dependency injection

There are three different types of dependency injection.

  1. interface injection in which the exported module provides an interface to be implemented in order to get the dependencies (values) at runtime.
  2. setter injection by which dependencies are injected using Java setter methods.
  3. constructor injection in which the dependencies are provided via the class constructor.

The benefits of dependency injection are...

  1. a reduction in the amount of boilerplate code in the application since initialization is handled by a provider component.
  2. configuration flexibility since alternative implementations of a given service can be used without recompilation—useful in JUnit testing since it's easy to fake injection.

Drawbacks...

  1. Code using injection seems "magical."
  2. Excessive or inappropriate use leads to greater complication of the application. It's harder to read and understand, harder to modify.
  3. IDEs have a hard time analyzing and/or refactoring it.

Initialization

In Eclipse, edit faces-config.xml and use the Managed Bean tab of the editor to create of your Java class a managed bean. Use the Initialization section to initialize the bean's properties even if they are a Map or List. In addition to the "managed-bean" specification (illustrated previously), a paragraph for "managed-property" can be added that looks like this:

	<managed-property>
	  <property-name>               firstName                     <property-name>
	  <property-class>  com.etretatlogiciels.jsf.treatise.Person  <property-class>
	  <value>                     #{firstName}                    </value>
	</managed-property>

On JSP and JSF

In JSF you access the values of a managed bean via value binding rather than writing code to call getters and setters.

JSP begins the concept of separating the efforts of an application between the model, the view and the controller. JSP already separates state between UI components; each component is aware of its data.

JSF introduces a separation of the functionality of a component from its presentation (or display). HTML rendering is responsible for for separate client presentation.

There are versions of JSF. JSF 1.2 is built directly atop JSP and uses JSTL tags. JSF 2.0 uses Facelets.

(Left off at 4.0.)