What is JSF (Java Server Faces)

Java Server Faces technology is a server side framework for building user interfaces for web applications. JSF framework as a toolbox  that is full of  ready to use components where you can quickly  and easily add and reuse  these components many times in a page and capture events generated by actions on these components. So JSF applications are event driven.

JSF  includes mainly:

Set of APIs to represent and manage state of components that helps server side validation, event handling, page navigation, data conversion etc.

JSP custom tag library to create UI components in a view page.

The UI (user interface) created using JSF technology runs on server and  output is shown to the client. Goal of JSFis to create web applications faster and easier. Developer can focus on UI components, events handling, backing beans and their interactions rather than request, response and markup. JSF hides complexities to enable developers to focus on their own specific work

JSF – Components

A JSF application primarily contains two types of components — both are easy to use and conforming to the POJO philosophy.

A JSF page is composed of XML tags. Each tag element corresponds to a UI component in JSF. As a web developer, you do not need to deal with the HTML markup or JavaScript details, as they are all generated from the JSF component tags. Since each UI component is self-contained and has well-defined behavior (i.e., it understand how to render itself and how to handle its data), JSF provides a very POJO-like experience for developing web UI.

Dynamic data on JSF pages and forms are modeled in POJOs known as JSF backing beans. The backing bean lifecycle is managed by the JSF server. For instance, a backing bean can have a session scope to track a user session. A backing bean can be dependency injected into another bean via an XML configuration file, and it can be injected into a JSF UI component via the JSF Expression Language (EL). The backing bean also provides the integration points between the web UI and the EJB3 business components.

JSF – Expression Language

What Is the Expression Language?

The expression language provides a way to simplify expressions in JSP. It is a simple language used for accessing implicit objects and Java classes, and for manipulating collections in an elegant manner. EL provides the ability to use run-time expressions outside of JSP scripting elements

<h:inputText id="name" value<strong>="#{student.name</strong>}">

Implicit objects are a set of Java objects that the JSP Container makes available to developers in each page. These objects may be accessed as bUilt-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.

  • pageContext – jsp:pageContext – Provides access to all the namespaces associated with a JSP page and access to several page attributes
  • Request – http.HttpServletRequestData – included with the HTTP Request
  • Response – http.HttpServletResponse – HTTP Response data, e.g. cookies
  • Session http.HttpSession – User specific session data

JSF – Executable Files (jars)

The JSF component model also allows third party vendors to develop and sell reusable UI component libraries. Those component libraries make it easy for server-side developers to take advantage of the latest browser technology without the need to mess with JavaScripts and such. JSF provides standard, reusable components for creating user interfaces for web applications.

JSF jar files are:

  • commons-beanutils.jar: Utilities for defining and accessing JavaBeans component properties
  • commons-collections.jar: Extensions of the J2SE Collections Framework
  • commons-digester.jar: For processing XML documents
  • commons-logging.jar: A general purpose, flexible logging facility to allow developers to instrument their code with logging statements
  • jsf-api.jar: Contains the javax.faces.* API classes
  • jsf-impl.jar: Contains the implementation classes of the JSF Reference Implementation
  • Odc-jsf.jar : Contains the Java widget library of the JSF implementation like graph.

JSF – Components

A JSF application is just like any other Java technology-based web application; it runs in a Java servlet container, and contains:

  • JavaBeans components (or model objects) containing application-specific functionality and data
  • Event listeners
  • JSP pages
  • Server-side helper classes
  • A custom tag library for rendering UI components
  • A custom tag library for representing event handlers and validators
  • UI components represented as stateful objects on the server
  • Validators, event handlers, and navigation handlers. (Validators are used to validate data on individual components before the server-side data is updated.)
  • Application configuration resource file for configuring application resources

JSF – Sample Page

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view> <f:loadBundle basename=“uk.com.web.properties.ResourceBundle_en_US" var="ResourceBundle"/> //for Stylesheet <LINK href="<%=PageCodeBase.staticContent%>theme/css/Master.css" rel="stylesheet" type="text/css"> //For java script
<script type="text/javascript" src="<%=PageCodeBase.staticContent%>js/common.js"></script>
<h:form id="helloform"> <h2>What is your name?</h2>
<h:inputText id="username" value="#{UserNameBean.userName}" validator="#{UserNameBean.validate}"/> <h:commandButton id="submit" action="success" value="Submit"/> </h:form>
</f:view> //For Extended JSF component
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<body bgcolor="white"> //For load the properties files