Google News
logo
JSF Interview Questions
JSF (JavaServer Faces) is a server-side component-based user interface framework.

It is used to develop web applications. It provides a well-defined programming model and consists of rich API and tag libraries.

The latest version JSF 2 uses Facelets as its default templating system.
The features are :
 
* Java framework built over Servlet API
* UI components are stored on the server
* Event-driven programming model that is handled on the server
* Supports multiple client devices
* Ease of implementing 3rd party components
* Extensible architecture
* Extensive tool support from Borland, Exadel, Sun, Oracle, IBM, BEA, etc.
* Rapid application development (RAD) approach
* Supports internationalization of UI labels and text
The life cycle contains 6 phases :
 
1) Restore view : The life cycle begins with the restore view phase. When a link or button is clicked on the web page, a request is sent to JSF with operations as listed below:
 
 * View is built by JSF
 * Connects event handlers and validators to user interface components
 * Saves this View in FacesContext instance
 * Provides all information to FacesContext to process request

2) Apply request : Here, each component from the component tree is created. Use the decode method that captures and saves new values from request parameters. In case of any failure during conversion, it generates an error message and lists it on FacesContext. It will display validation messages or errors during the render response phase. JSF moves to render response phase when decode method event listeners call renderResponse method.
 
3) Process validation : During this phase, JSF processes component tree validators, examines attribute rules for validation, and compares them with the stored local value of the component. In case of invalid local value, JSF adds an error message to FacesContext instance, displaying the same page with an error message and with JSF life cycle moving further towards render response phase.
 
4) Model value update : After JSF verifies valid data, it sets the corresponding server-side object properties to the component’s local values and updates the bean properties, corresponding to the value attribute of the input component. When renderResponse from the current instance of FacesContext is being called by updateModels methods, JSF moves to the render response phase.
 
5) Invoke application : Application-level events like submitting form or forwarding to another page are being handled by Java Server Faces during this phase.
 
6) Render response : In case the application has JSP pages, JSF requests the application server to render the page, thereby adds components on the page to the component tree. Otherwise, an already built component tree need not add any components; JSP container move tags and renders components. The response state is saved after view content is rendered, making successive requests can access the state and its availability to restore view phase.
In the execute phase, when the first request is made, the application view is built or restored. For other subsequent requests other actions are performed like request parameter values are applied, conversions and validations are performed for component values, managed beans are updated with component values, and application logic is invoked.
Managed Beans are a Java class that consists of a set of getter/setter methods and properties. It acts as a model to the JSF framework and offers functionalities like,
 
* Component data validation
* Manage the execution of a component event
* Processing to decide what page application navigates next
* JSF use managed beans either by configuring them into an XML file or through annotations.

Following scopes are used for managed bean class :
 
* Application
* Session
* View
* Request
* None
* Custom
Managed Beans are Java beans that comprise getter and setter methods and have business logic designed in it. It may optionally have a backing bean. They act as a Model for UI components in the MVC framework.
 
For JSF 1.2, it is required to register them in the JSF configuration file, faces-config.xml, whereas, from JSF 2.0 onwards, annotations are used to register managed beans.
Facelets are lightweight page declaration language which uses HTML style templates to build component tree to build Java server faces views.
 
Some of Facelets feature are listed as below :
 
* It uses XHTML for creating web pages.
* Besides JSF and JSTL tag libraries, it offers Facelets tag libraries.
* Expression language support, component, and pages templating.
* Fast compile time
* High-performance rendering
* Extends its functionality to components and other server-side objects via customization
* Support for code reuse through composite components and templating
* Compile-time EL validation
Some of the Facelet tags used in JSF are listed below :
 
* ui: component defines the created component and adds to the component tree.
* ui: composition defines page composition, uses templates optionally, and ignores content outside.
* ui: debug defines debug component created and adds to the component tree.
* ui: decorate is similar to component tag, consider content outside this tag.
* ui: define is used to define content inserted into the page by a template.
* ui: fragment is similar to component and decorates tag. It considers content outside this tag.
* ui: include reuses and encapsulates multiple page content.
* ui: insert as name inserts contents into a template.
* ui: param pass parameters to the included file.
* ui: repeat an alternative for loop tags, like c: forEach or c: dataTable.
* ui: remove is used to remove content from a page.
The three types of text field tags are ;
 
<h:inputText> : This adds the text box next to the label field.
<h:inputSecret> : This type is used for password fields where the entered data is hidden.
<h:inputTextarea> : This type of fields is used while entering large number of characters.