Updated March 14, 2023
Introduction to JSF Life Cycle
JSF can be abbreviated as Java Server Faces is a type of Java-based Server-Client communication that facilitates the request and response flow through the system. The multiple phases that any request/ response has to go through in the JSF Life Cycle are Restore View Phase, Apply Request Value, Process Validations Phase, Update Model Values Phase, Invoke Application Phase, and Render Response Phase, in the same order of process flow.
Working of JSF Life Cycle
The working of the JSF Life cycle is as follows:
1. Server-side
The code you write in a JavaServer Faces (JSF) application runs on the server, but components can also include JavaScript code that runs on the client. You can contrast this with client-side frameworks like Angular where you write code in JavaScript or Typescript which runs on the client-side in the web browser.
The fact that it’s a server-side framework does not mean that you cannot use the client-side code in the JSF application. In fact, JSF components can include JavaScript and JSF support for AJAX, for communication, the team client-side and server-side code.
2. Component-based
JavaServer Faces web applications are built using reusable, self-contained components as building blocks, making building web apps faster and easier.
The most interesting aspect of JavaServer Faces, which sets it apart from action-based web application frameworks, Such as Spring Web MVC (Model-View-Controller), Struts or plain Servlets in JavaServer pages is that it’s Component-based. This means that in JSF the building blocks of your application are reusable and self-contained components.
A Component can be any part of a web application. It can be as simple as a standard HTML form-control, such as a text input field or a button, or as complex a table with sorting and pagination support.
Example:
A calendar or a chart or any other part of a web application that you can think of the framework manages the components on a page and takes care of a lot of things such as taking user input from form fields, validating and converting data, and storing it into model objects and invoking actions.
JSF comes with a number of standard components and there are also libraries available with many ready-to-use components.
3. Web UI framework
JavaServer Faces is focused on the user interface part of web applications – you define pages in the Facelets view definition language.
The standard view definition language used in JSF is called Facelets.
Different Regular Phases of JSF Lifecycle
There are six different regular phases inside the JSF Life Cycle. The above diagram shows the typical order where the six phases perform.
1. Restore View Phase: This phase begins each time a consumer requests a JSF page by simply clicking on a link, button, and so on. In this page view generation, the binding of components to the event handlers and validators is performed as well as the view is preserved within the FacesContext object.
2. Apply request value: The objective of this phase is made for each component to retrieve the current state
3. Process Validations Phase: In this phase, local values stored to get the component in the tree will be in comparison to the validation rules authorized to get the components.
4. Update Model Values Phase: Following verifying that data is valid in the last phase local values of elements could be started related to server-based object properties, for example, backing beans.
5. Invoke Application Phase: Ahead of this phase the component values had been transformed, validated, as well as, put on the bean objects so that you can utilize these to perform the application’s business logic.
6. Render Response Phase: This phase fetches a new view if needed using values from server-side beans then we output the view using the values that are in the tree and then for beans that are not on the request in other words on the session scope or application scope we will then save those the current state.
Understanding Faces Flows with demo application in a real-life example
Below is the demo application of face flows with an example:
The Checkout Process
- Enter shipping details
- Name and Address
- Enter payment details
- Name, credit card type, and number
- Review order
- Confirm or Cancel the order
- Payment succeeded or failed
Let see Checkout Flow with Flow Nodes in the below diagram.
A flow is consisting of a number of nodes. Each node is stepped in the flow that performs some function and it specifies which node should be called next. In the definition of a flow, you define the nodes and the rules for navigating between them. In this diagram, you see the nodes of the checkout flow.
It starts with shipping, then payment and review then the execute node which is where the payment is made and the order is fulfilled and finally check result success or failure, and then the process is finished.
Kinds of Flow Nodes in JSF Life Cycle
Following are some kinds of flow nodes in the JSF file cycle:
Nodes in JSF
There are five different kinds of nodes in JSF Life Cycle:
- View node: Display a view (page)
- Method call node: Call a method
- Switch node: Select one of a series of cases
- Return node: End the flow
- Flow call node: Call another flow
Flow Methods
Below are similar flow methods in the JSF life cycle:
- Entry point, Parameters, Exit points: May return one or more values
- Flow Scope: Flow-scoped beans are like local variables
- Nested Flow Calls: Call stack
A call stack is used to keep track of invocations and scopes of flows, exactly like how it works with the method When a flow is called a new entry is pushed into the stack, and when flow returns its entry is popped from the stack.
Configuration of Flow
There are two ways to specify the configuration of flow:
- XML Configuration File: In faces-config.xml or in a separate file
- In Java using the Flow Builder API: CDI producer method with @FlowDefinition annotation
Conclusion
Java Server Faces (JSF) is the standard concept for establishing web applications in Java. Even so, throughout the development as well as, deployment of JSF applications, there are numerous performance and configurations that need to be applied to help make the applications safer, reliable, as well as, effective.
Recommended Articles
This has been a guide on JSF Life Cycle. Here we have discussed the Six Different Regular Phases of the JSF lifecycle and five different kinds of nodes in the JSF Life Cycle. You may also look at the following articles to learn more –