JSF

26. How managed beans are initialized in JSF ?

You never have to manually call new() on a managed bean. Rather, managed beans are lazily initialized by the container at runtime, only when needed by the application.

27. What kind of class can be declared as managed bean in JSF ?

Any Java class with a public, no-argument constructor that conforms to the JavaBeans naming conventions for properties can be registered as a managed bean. Objects of type java.util.List and java.util.Map can also be registered as managed beans.

28. How to declare manged bean in faces-config.xml file in JSF ?
POJO class

public class UserBean { }

faces-config.xml

<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.roytuts.model.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

29. How do you initialize managed bean’s property in JSF ?

It is possible to supply initial values to managed beans by adding a @ManagedProperty annotation on a field or by providing a <managed-property> element inside the managed-bean entry in the Faces configuration file. For example, to initialize the firstName and lastName properties of the userBean managed bean, you can add the following to the annotation declarations:

@ManagedBean
@SessionScoped
public class UserBean {
@ManagedProperty(value="Soumitra")
private String firstName;
@ManagedProperty(value="Roy")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

or the following to the configuration:

<managed-bean>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.jsfcompref.register.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>firstName</property-name>
<value>Soumitra</value>
</managed-property>
<managed-property>
<property-name>lastName</property-name>
<value>Roy</value>
</managed-property>
</managed-bean>

30. What are the immediate and deferred expressions in JSF ?

In JSP, any ${} expression that appears in the page is evaluated immediately as the page is rendered. This is called immediate expression.

JSF introduced the deferred expression concept to allow expressions to be useful both during the rendering of the page, and during a postback. This concept allows a deferred expression – #{}.

The #{} delimiter was chosen because it prevents the JSP runtime from evaluating the expression, allowing the JSF JSP Custom Tag handler to create the expression instance and store it in the component for later evaluation.

31. Why expression language (EL) is used ?

The goal of having a Unified EL is to provide an easy and compact way to access application objects from any point in the application.

Consider a tag with an attribute:

<MyTag attribute=”value” />.

If value had to be derived dynamically at runtime, it could necessitate a long Java statement to obtain the value. Expression language provides a solution to this by

1. Eliminating the need to always refer to container-managed parent objects (request, session, and application).
2. Shortening the expression by omitting get and set from object properties/methods.
3. Allowing navigation of an arbitrarily complex hierarchy of JavaBeans objects.

32. What is rendered attribute in JSF ?

The rendered attribute evaluates a boolean value and based on either true or false it decides to display the element or not to display the element to the end user.

Suppose if a user has admin role then only show edit action using the below commandButton

<h:commandButton action="#{adminBean.edit}" value="Edit" rendered=#{adminBean.isAdmin}/>

So admin is a boolean field in managed bean class AdminBean and if the value of admin field is true then the above button is displayed to the user for edit operation.

33. What is flash in JSF ?

David Heinemeier Hansson, the creator of Rails, chose the term “flash” for its usage as in “flash memory”. In this sense, flash is short-term storage. Specifically, anything you put into the flash on one request will be available from the flash on the “next” request from the same browser window. This is true whether the next request is a JSF postback, a redirect, or even a simple HTTP GET for a new page.

34. What is backing bean in JSF ?

You can generally create a Java class for each JSF page and register it as a managed bean. It is recommended that backing beans be declared to be in request scope. The most preferred usage is to have a single backing bean per page although this is not enforced by any specification. A common usage is also to name the class the same name as the page.

The backing bean holds the following artifacts for a page:

Properties corresponding to input fields on a page, such as string properties for userid and password.

Action methods and action listeners that correspond to UI components that initiate action events, such as clicking Register or Next Page.

Declarations of UI component instances that can be directly bound to the UI components used on the page.

35. What is <redirects/> in JSF ?

A <redirect/> option in a navigation case triggers a slightly different type of navigation. Specifically, the redirect option causes the client browser to make a new HTTP request for the specified view as opposed to just rendering the response without requiring a separate HTTP request.

If you do not use <redirects/> in your navigation rules then you will see the old URL in the browser’s address bar even if you are shown content from different URL.

Whether or not to use redirects largely depends on whether what is shown in the address bar of the browser matters or could be confusing if the page referenced no longer matches the page being rendered. Another important consideration of when to use redirects is performance. Using a redirect will terminate the current request and cause a new request response cycle to occur. If the page has a very large set of components, this could have a noticeable performance impact. Redirects can also necessitate an extra round trip to re-instantiate any request-scoped objects that have been disappeared by the next request.

36. What is keep keyword in flash ?

keep keyword is used along with flash when we want to persist the value for longer time than N+1 requests, where N is the request number for which we kept the value in flash.

37. What is validation in JSF ?

Validators are instances of javax.faces.validator.Validator. Validation guarantees that it is valid given the application-specific constraints. Validators may only be associated with input components.

Validation normally happen during the Process Validations phase of the request processing lifecycle unless the component has its immediate property set to true, in which case they happen during the Apply Request Values phase.

38. What is convention in JSF ?

Converters are instances of javax.faces.convert.Converter. Conversion guarantees the data is in the expected type. Converters can be associated with input or output components.

Conversion (from Object to String) happens during the Render Response phase of the request processing lifecycle.

39. How to validate minimum and maximum numeric value in JSF ?
Assume that the number field in bean is numeric. So check whether the input value is between 1 and 10.

<h:inputText value="#{bean.number}" id="numberField" required="true">
<f:validateLongRange minimum="1" maximum="10" />
</h:inputText>