Important JSF (Java Server Faces) Features

A Quick Recap

In the previous tutorial, we have seen the Components of JSF. You have also learned that there is a UI Component that can be identified by a tag, and each component has its one or many renderers. As you know, JSF extends JSPs. Servlets and Plain Java Classes along with UI Component Models, UI Component Class, and UI Component Renderer Class can make JSF. You have also seen a misconception with JSF. 
In this tutorial, we will discuss various JSF Features and try to understand Validation, type conversion Error handling, event handling and Various Listeners that exists in Java Server Faces Framework. These UI Components and Features provides us the Reusability in JSF.

List of JSF Features

  • Specification of Page Navigation
  • UI Components
  • Type conversion
  • Validation of User Input
  • Error handling
  • Management of Java Beans with JSF
  • Handling of an Event
  • Internationalization

Specification of Page Navigation in JSF

Any web application that contains more than one page needs a mechanism for the navigation of pages. In JSF page navigation is done with page navigation rules which are defined in the application configuration file named “faces-config.xml” . We can have two type of page navigation in JSF.

  • Simple page navigation
  • Conditional page navigation

Syntax for Simple page navigation

In this case, no conditions should be fulfilled before the redirection

Syntax for conditional page navigation

In this case, conditions should meet before the redirection.

How JSF Performs Navigation?

To understand this let’s discuss it with a scenario. On the button click and action event is created and this event is handled by the listens of the component, this listener then calls the action method of that particular component. JSF performs all these tasks. No application developer should provide an implementation of the Action Method in the backing beans. Action Method returns the Logical outcome string, and the string keeps the result of the processing. This outcome is then used by the Navigation handler to display the next page. Navigation handlers match the outcome with the navigation rules for the successful navigation.

User Input Validation

Whenever there are Inputs, it has to be validated to respond accordingly. Validators are invoked in Process validation phase.  There can be two possibilities after validation process. First one is successful validation and the second one is unsuccessful validation.

In the case of No success, a component-specific error message is added in the Faces Context instance. By using the Method binding concepts validations can also be delegated to the managed bean.

Do you know what is managed bean?

We can say to communicate JSP page with Java we have managed bean. Managed bean can be used to validate form values.

Declarative and Imperative validations

JSF also do Declarative and Imperative validations. Declarative validation is the standard validation while the Imperative validation is the Custom validation. Managed bean do the Declarative validation, and Imperative validation is done by implementing javax.faces.validator.Validator

JSF also provides some other validators as well, which developers can use.

  • Double range validator: numeric validator
  • Long range validator: numeric validator convertible to long
  • Length Validator: string validator

Type Conversion In JSF

When string data is converted into the Java object, it then calls the conversion process. JSF can associate a component with the server side object data. This object can be a backing bean. When we associate a component with the object, then there are two possible views of the component data. Model View and the Presentation View. In the Model View, data is present in the form of strings or int and the Presentation View, the data is present in the modifiable form.

How JSF Performs Conversion?

JSF automatically converts the data in model view as well as presentation view. You can also write your custom converters. You must do the following to define a custom converter.

  • Implement Converter Class
  • Register the converter with application
  • Target component must have the reference of the converter in its component tag defined by the page author.

Error Handling in JSF

Previously, you have seen in the Lifecycle phase tutorial that whenever an error occurs it is added in the facesContext instance.

The Error Response depends on the Error severity. JSF displays the error message in HTML Message Component. In JSF request processing lifecycle there are several other points where the error arises.

Java Bean management

We configure our java beans in the “faces-config.xml” file with the help of managed-bean elements. This managed-bean element from the faces-config.xml is then used with the JSF to create instances of the java beans.

sample faces-config.xml

Event Handling in JSF

JSF is an event-driven application. It controls the actions of the user, so we use event listeners in JSF. These event listeners are written in managed-bean or Java-bean. On any user event, a corresponding event handler is created and added to the event list. Then the specific component fires the event.

It means that JSF looks in the event list for that specific event listener and call the event notification method of that listener/handler instance. Developers must provide the implementation of these event listeners.

To write an event listener, the developer should follow below-given steps.

  • Create a listener class
  • Place it in WEB-INF folder
  • Use action_listner or valuechange_listner.
composition of JSF event class
the composition of JSF event class

From the above-given figures, we can easily see that how the event objects are made.

  • Action Event: use to represent the action of the UI components.
  • Value Change Event: use to represent the change of the local value of the UI components.

Events are present for the UI components; we just have to listen to it. We can listen to these events with event listeners.

composition of event Listners
Composition of Event Listeners

The figure shows the composition of the event listener.

Summary

In this tutorial, we have accomplished following things

  • A short recap of the previous tutorial
  • Features of the Java server faces
  • A brief explanation on Features the JSF Components

In the next post, you will learn about “Facelets” in JSF in detail.

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About AuthorUzair Ahmed Khan is a Technical Content Writer, Computer Software Blogger and Software Engineer Enthusiast with a keen eye on Java Technologies other Related Developments.

 

Leave a Reply

Your email address will not be published. Required fields are marked *