19 JSF Important Interview Questions
Table of Contents
19 JSF Important Interview Questions:
JSF(Java server faces) is component driven programming language which can be implemented with different faces like icefaces, acefaces, primefaces, richfaces etc. This can also be used with spring core and hibernate for ORM. Below are the few most important and repeated interview questions for JSF developers.
1. What are all the 6 phases of JSF Lifecycle ?
1. Restore view
2. Apply request values
3. Process validations
4. Update model values
5. Invoke application
6. Render response
2. What is FacesContext or facescontext instance ?
Facescontext is responsible to keep all the information’s to process the single request.
3. What are all the scopes of JSF ?
Scope represents the lifetime of the bean.
@SessionScoped
@RequestScoped
@ApplicationScoped
@ViewScoped
Above scopes are the most used scopes, apart from these scopes JSF also supports @FlowScoped (which supports the collection of views which we configured in the flow configuration file.)
Remember: JSF 2.x supports only above mentioned 4 scopes.
4. List down the few tag libraries supported by Facelets ?
JavaServer Faces Facelets Tag Library [Tags for templating]
“http://java.sun.com/jsf/facelets” | Prefix:ui | Eg: ui:component, ui:insert
JavaServer Faces HTML Tag Library [JSF component tags for all UIComponent objects]
“http://java.sun.com/jsf/html” | Prefix:h | Eg: h:head,h:body,h:outputText,h:inputText
JavaServer Faces Core Tag Library[Tags for JavaServer Faces custom actions that are independent of any particular render kit]
“http://java.sun.com/jsf/core” | Prefix:f | Eg: f:actionListener,f:attribute, f:ajax
JSTL Core Tag Library [JSTL 1.2 Core Tags]
“http://java.sun.com/jsp/jstl/core” | Prefix:c | Eg: c:forEach,c:catch
JSTL Functions Tag Library [JSTL 1.2 Functions Tags]
“http://java.sun.com/jsp/jstl/functions” | Prefix:fn | Eg: fn:toUpperCase,fn:toLowerCase
5. Difference between managed bean and backing bean ?
Backing Bean: Bean that is bounded with UI components / referenced by form.
Managed bean: Any java bean objects created with JSF implementation like below,
In JSF 2.0 we have @ManagedBean,
[java]
@ManagedBean(name=”managedBean”)
@RequestScoped
public class BackingBean{
// some code here
}
[/java]
In other terms, We can say backing bean is a class where managed bean is an instance.
6. How to choose the right scope ?
@RequestScoped: Scope for each get/post request. Bean exist till the request is exist.
@ViewScoped: For dynamic and ajax based validation/views. Bean exists till the view exists.
@FlowScoped: Navigating to the list of views as configured in the flow configuration file. ( From Jsf 2.X flowscoped is not used much and even not recommended)
@SessionScoped: For user specific data like last time logged in, language etc.. (mainly used for logged in users).
@ApplicationScoped: For application wide data/constants, such as dropdown lists which are the same for the entire application.
7. List down the few interfaces in JSF ?
Converter and Validators are the mostly used interfaces in JSF. In Jsf 1.2 both converter and validator was configured in XML files.
But in JSF 2.X they are configured with the below annotations,
[java]
@FacesConverter(“appConvert”)
@FacesValidator(“formValidate”)
@ManagedBean @RequestScoped
public class LoginController{
}
[/java]
Validator can be used to validate the input data’s and converter can be used to convert string to date or date some format to required format etc.,
8. How do you handle viewExpiredException in JSF (part of session expiry feature) ?
We need to add the below entry to our faces-config.xml file to expire the view or session expire, we have to also mention the session timeout.
[xml]
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/home.xhtml</location>
</error-page>
[/xml]
9. How JSF Framework works ?
JSF follows MVC framework.
Model: Hibernate, JPA and data beans for persistance.
View: XHTML, JSP pages for view.
Controller: Managed bean with business logics to communicate model and view layers.
10. What is f:ajax in JSF ?
f:ajax exists in the JavaServer Faces Core Tag Library, when we use f:ajax which internally loads javascript resource library.
<h:inputText value=”#{formBean.name}”>
<f:ajax />
</h:inputText>
here, for h:inputText ajax is enabled but render,execute or any other attributes are not defined. When no event attribute is specified then default behavior of the component is performed. So here valueChange event will be performed for inputText component.
11. What are all the attributes of f:ajax Tag ?
disabled : to disable the ajax functionality [Default: false]
render : to render the other components on the client.
execute : to execute the other components on the server.
immediate : to execute in apply request value phase(phase 2 when immediate=”true” this will be executed) than invoke application phase(default).
event : to trigger the events.
default f:ajax events are,
“action” for javax.faces.component.ActionSource components
“valueChange” for javax.faces.component.EditableValueHolder components.
listener – listener method to be called when AjaxBehaviourEvent triggered.
12. Explain “execute” and “render” attribute of f:ajax ?
Execute attribute can be used to control the components to be executed on the server.
execute attribute can have any of this value,
@this, @all, @form, @none or ID_OF_THE_COMPONENT
Default value for f:ajax “execute” attribute is @this.
Render attribute can be used to control the components to be rendered on the client.
render attribute can have any of this values,
@this, @all, @form, @none or ID_OF_THE_COMPONENT
Default value for f:ajax “render” attribute is @none.
13. What f:ajax immediate=”true” will do ?
As we know JSF has 6 phases in it’s life cycle.
1. Restore view
2. Apply request values
3. Process validations
4. Update model values
5. Invoke application
6. Render response
By default f:ajax will happen in invoke application phase(5th phase). But when you put immediate=”true” attribute to f:ajax tag then it will happen at apply request values phase(2nd phases) itself.
14. What is Eager Application-scoped Beans ?
JSF Managed beans are instantiated(lazily) when a request is made from the application. When you want to instantiate the managed bean when the application started itself and even before any request is made then eager attribute should be set to true as below,
@ManagedBean(eager=true)
@ApplicationScoped
15. How do you inject the managed beans in JSF 2.0 ?
In JSF 2.0, @ManagedProperty annotation is used to define the dependency of the managed bean to be injected in another managed bean.
LoginBean.java – A managed bean named “login“:
[java]
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name=”login”)
@SessionScoped
public class LoginBean implements Serializable {
//business logic and whatever methods…
}
[/java]
Now injecting the loginBean in userBean like this,
UserBean.java
[java]
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserBean implements Serializable {
@ManagedProperty(value=”#{login}”)
private MessageBean loginBean;
//
public void setLoginBean(LoginBean lgnBean) {
this.loginBean =lgnBean;
}
//…
}
[/java]
In this example, it uses the @ManagedProperty annotation to DI the “login” bean (LoginBean.java) into the property (loginBean) of the “UserBean” via setter method,setLoginBean().
16. What is Phase Listener ?
Phase Listener is an interface implemented by any class/object that like to be notified at the beginning and ending of each phase of the life cycle.
Methods:
public void afterPhase(javax.faces.event.PhaseEvent event)
Handle a notification that the processing for a particular phase has just been completed.
public void beforePhase(javax.faces.event.PhaseEvent event)
Handle a notification that the processing for a particular phase of the request processing lifecycle is about to begin.
public javax.faces.event.PhaseId getPhaseId()
Return the identifier of the request processing phase during which this listener is interested in processing PhaseEvent events. Legal values are the singleton instances defined by the PhaseId class, including PhaseId.ANY_PHASE to indicate an interest in being notified for all standard phases.
17. Difference between action and action listener in JSF ?
action: action can be used to execute the business actions and to handle the navigations. action methods returns string which is matched with the navigation case (from outcome) to target view (to view).
A return value can be null/empty string/void/same view id. For null and void it will return the same page with current view data and scope alive. But for empty string and same view it will again return the same page but destroy all the existing data and recreate the new/fresh view page.
actionListener: actionlistener can be used to perform some action before the real business action like logging, some set property using ( <f:setPropertyActionListener>) and for some access to the component (actionEvent as method argument).
When you are using both action and actionListener, then always actionListener will be called before action.
18. Difference between h:link, h:commandLink & h:outputLink?
h:outputLink: It cannot directly invoke a managed bean action method.
<h:outputLink value=”login.xhtml”>Click to Login</h:outputLink>
h:commandLink: It can invoke a managed bean action method. It’s also required to be placed inside a <h:form>
<h:form>
<h:commandLink value=”Java Tutorial Site” action=”javadomain” />
</h:form>
h:link: The <h:link> which can take a view ID (a navigation case outcome) instead of an URL. It will generate a HTML <a> element as well with the proper URL in href.
<h:link value=”link text” outcome=”destination” />
Prefer h:commandLink where ever you need to fire a POST request and data needs to be processed on the server.
Prefer h:link for pure page to page navigation and also to handle GET request. But no validation action and conversions. Also no immediate and execute attributes.
19. How do you integreate JSF with Spring ?
Add the faces or JSF entry in web.xml:
[xml]
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
[/xml]
Adding Spring El Resolver entry in faces-config.xml file:
[xml]
<?xml version=’1.0′ encoding=’UTF-8′?>
<faces-config version=”2.2″
xmlns=”http://xmlns.jcp.org/xml/ns/javaee”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd”>
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
[/xml]
Add the below Listeners to web.xml to load application context for Spring:
[xml]
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
[/xml]
Add faces-config. xml location in web.xml: (more than one faces-config.xml can also be used)
[xml]
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config1.xml,/WEB-INF/faces-config2.xml</param-value>
</context-param>
[/xml]