JSF Interview Questions

How to reload the page after ValueChangeListener is invoked?

At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()

How to download PDF file with JSF ?

This is an code example how it can be done with action listener of the backing bean.

Add the following method to the backing bean:

public void viewPdf(ActionEvent event) {
String filename = "filename.pdf";
// use your own method that reads file to the byte array
byte[] pdf = getTheContentOfTheFile(filename);
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType("application/pdf");
response.setContentLength(pdf.length);
response.setHeader( "Content-disposition", "inline; filename=\""+fileName+"\"");
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}

This is a jsp file snippet:

<h:commandButton immediate=”true” actionListener=”#{backingBean.viewPdf}” value=”Read PDF” />

How to show Confirmation Dialog when user Click the Command Link?

h:commandLink assign the onclick attribute for internal use. So, you cannot use it to write your own code. This problem will fixed in the JSF 1.2. For the current JSF version you can use onmousedown event that occurs before onclick. <script language=”javascript”> function ConfirmDelete(link) { var delete = confirm(‘Do you want to Delete?’); if (delete == true) { link.onclick(); } } </script> . . . . <h:commandLink action=”delete” onmousedown=”return ConfirmDelete(this);”> <h:outputText value=”delete it”/> </h:commandLink>

What is the different between getRequestParameterMap() and getRequestParameterValuesMap() ?

getRequestParameterValuesMap() similar to getRequestParameterMap(), but contains multiple values for for the parameters with the same name. It is important if you one of the components such as <h:selectMany>.

What are the available implementations of JavaServer Faces?

The main implementations of JavaServer Faces are:

  • Reference Implementation (RI) by Sun Microsystems.
  • Apache MyFaces is an open source JavaServer Faces (JSF) implementation or run-time.
  • ADF Faces is Oracle’s implementation for the JSF standard.

Explain what do you mean by a view object?

A view object is a model object used specifically in the presentation tier. It contains the data that must display in the view layer and the logic to validate user input, handle events, and interact with the business-logic tier. The backing bean is the view object in a JSF-based application. Backing bean and view object are interchangeable terms.

Explain what do you mean by a Managed Bean?

JavaBean objects managed by a JSF implementation are called managed beans. A managed bean describes how a bean is created and managed. It has nothing to do with the bean’s functionalities.

Explain what do you mean by a Backing Bean?

Backing beans are JavaBeans components associated with UI components used in a page. Backing-bean management separates the definition of UI component objects from objects that perform application-specific processing and hold data.
The backing bean defines properties and handling-logics associated with the UI components used on the page. Each backing-bean property is bound to either a component instance or its value. A backing bean also defines a set of methods that perform functions for the component, such as validating the component’s data, handling events that the component fires and performing processing associated with navigation when the component activates.

Explain what does a typical JSF application consists of?

A typical JSF application consists of the following parts:
JavaBeans components for managing application state and behavior.
Event-driven development (via listeners as in traditional GUI development).
Pages that represent MVC-style views; pages reference view roots via the JSF component tree.