JSF Interview Questions and Answers

What is JSF?

JSF stands for Java Server Faces. It is a UI component based and event driven MVC web application framework which simplifies the UI construction by reusable UI component. JSF specification provides a set of standard UI components which are reusable and extendable.
Note: JSF is built on top of the Servlet API.
See more at: JSF overview.

Explain JSF framework architecture.

In JSF application JavaBean classes are act as a model and contains data and business logic. Commonly a JSP or Facelets (XHTML) page is used for view. JSF components are used to bind the view and model. FacesServlet act as a controller and use component tree to do all task like get request, gathering, validating and converting user input, put it into model objects, action invocation and render response.

Explain JSF lifecycle phases.

The JSF application consist of six distinct phases given below:
1. Restore View
2. Apply Request Values
3. Process Validations
4. Update Model Values
5. Invoke Application
6. Render Response
See more at: JSF lifecycle phases.

What is managed Bean?

A managed bean is a normal java bean class which contains data and business logic. A managed bean is managed by JSF framework and act as a model for JSF application. Before using a managed bean we have to configure it either in faces-config.xml file or using annotation.
See more at: JSF managed bean.

What is backing bean?

JSF backing bean is a managed bean which contains some or all values of a web form. It is normally for business logic.
See more at: JSF backing bean.

Explain jsf bean scopes.

Bean Scopes:
When we define a bean, we have to define its scope also in which it will be placed. JSF implementation provide us serval scope for a bean. Request scope is the default scope.
Commonly used bean scopes:
1. Request scope.
2. View scope.
3. Session scope.
4. Application scope.
See more at: JSF bean scopes.

Explain jsf configuration files.

faces-config.xml:
The faces-config.xml file contains the JSF application configuration. It can be used to configure the following:
1. Used to configure managed beans.
2. Used to configure convertors.
3. Used to configure validators.
4. Used to configure navigation.

What are the different types of Page Navigation supported in JSF?

Navigation:
Navigation in programming is way of transferring the control from one view to another.
Navigation rules in JSF framework:
JSF framework defines some navigation rules based on those view is render after an action is performed. A navigation rule can be defined in JSF configuration file faces-config.xml or in managed bean. JSF 2 framework provides the facility of implicit navigation for which no need to define navigation rules.
Types of navigation in JSF framework:
1. JSF implicit navigation rule.
2. JSF conditional navigation rule.
3. JSF from-action navigation rule.
4. JSF page redirect rule.
See more at: JSF navigation rule.

What is the significance of @ManagedProperty annotation?

The @ManagedProperty annotation is used for injecting a bean into a property of another bean. The @ManagedProperty annotation uses setter method of the bean which is used as a property into another bean. So we must have to provide setter method of that property. Value attribute contains the name of the bean to be injected.

What are different JSF Converter tags?

JSF converter tags:
JSF provides inbuilt convertors to transform the UI component data from strings to Java objects used in a managed bean and vice versa. For example a date object is represented by a text string on web form which can be converted back to the date object by using converters.
For using JSF converter tags we have to use the following namespaces of URI in html node.
<html 
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
>
Commonly used JSF converter tags:
TagDescription
  1. f:convertNumber
It is used to converts a string into a number of specific format
  1. f:convertDateTime
It is used to converts a string into a date of specific format
  1. Custom Convertor
It is used to create a custom convertor
See more at: JSF converter tags.

What are different JSF validator tags?

JSF validator tags:
JSF provides inbuilt validators to validate the UI component data. JSF validators can validates length, type, format etc.
For using JSF validator tags we have to use the following namespaces of URI in html node.
<html 
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
>
Commonly used JSF validator tags:
TagDescription
  1. f:validateLength
It validates length of a string
  1. f:validateLongRange
It validates range of numeric value
  1. f:validateDoubleRange
It validates range of float value
  1. f:validateRegex
It validates JSF component with a given regular expression.
  1. f:validateRequired
It validates that the input field is not empty. 
  1. Custom Validator
It creates a custom validator
See more at: JSF validator tags.

What are different JSF Event Handlers?

JSF event handling:
In JSF when a button is clicked or change value in text filed etc then corresponding JSF component create an instance of the corresponding event class and adds the event to the event list. After adding the event in the event list JSF component call the corresponding listener or handler method.
Note: JSF also provides the facility to handle the application level events which happens when application start or stop.
JSF Events:
Event HandlersDescription
  1. valueChangeListener
JSF fired value change events when user make changes in input components.
  1. actionListener
JSF fired action events when user clicks on a button or link component.
  1. Application Events
JSF fired application events when application start or stop or during JSF lifecycle: PostConstructApplicationEvent, PreDestroyApplicationEvent, PreRenderViewEvent.
See more at: JSF validator tags.

How to create custom tag in JSF?

JSF custom tag example:
JSF provides the facility to create custom tag to render a pre-defined content. A JSF custom tag uses “ui:composition” to insert content into the page.
Steps to create the JSF custom tag:
1. First create a predefined content in an XHTML page with ui:compisition tag.
2. Declares and define the custom tag details in a tag library descriptor.
3. Register the tag library descriptor in the web.xml file.
See more at: Jsf custom tag.

How to create composite components in JSF?

JSF provides the facility to create reusable custom components also known as composite components. Below are the steps to create composite components in JSF.
Steps to create and use the JSF composite components:
1. First create a resources folder.
2. Create an xhtml file in resources folder and declared the composite namespace.
3. Uses composite tags composite:interface, composite:attribute and composite:implementation, to define content of the composite component. The composite:interface tag is used to declare the configurable values. The composite:implementation tag is used to declare the XHTML markups. To access composite:interface attribute we can use #{cc.attrs.attributeName}.
4. For using the composite component create an xhtml file and use custom component’s namespace http://java.sun.com/jsf/folderName where folderName is the name of the folder under resources forlder containing the composite component.
5. Use the composite component like a normal JSF component.

How to terminate the session in JSF?

public String logout() {
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
session.invalidate();
return "loginPage";
}

Is it possible to have more than one Faces Configuration file?

Yes. We can define the list of the configuration files in the web.xml.
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config-navigation.xml,/WEB-INF/faces-beans.xml</param-value>
</context-param>

No comments: