Table of contents
1.
Introduction
2.
JSF
2.1.
1. Server-side
2.2.
2. Component side
2.3.
3. Web UI Framework
3.
JSF Life Cycle
4.
Execute Phase
4.1.
1. Restore View Phase
4.2.
2. Apply Request Values Phase
4.3.
3. Process Validations Phase
4.4.
4. Update model values
4.5.
5. Invoke application
4.6.
6. Render Response Phase
5.
Render
6.
Frequently Asked Questions
6.1.
What are Facelets in JSF?
6.2.
What are controllers in JSF Application?
6.3.
Which tag does JSF use to upload a file to a website?
6.4.
What are the various bean scopes available in JSF?
6.5.
What are the two phases of the life cycle?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

JSF Life Cycle

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas!!

While working on web interfaces, the main problem arises when the developer is new to client-side technologies like HTML, CSS, and Javascript. To solve this, the concept of JSF comes in. A framework like JSF's primary goal is to encapsulate (or wrap) client-side technologies like HTML, CSS, and JavaScript so that programmers can create web interfaces with little to no interaction with these tools. And when a response or request is made, it goes in a certain flow or a phase. It is known as the JSF Life cycle. Let's go on the journey to learn more about it.

 

 

Now First, let's have a look at JSF.

JSF

The Java standard technology for creating component-based, event-oriented web interfaces is called JavaServer Faces (JSF). JSF permits server-side data and functionality access, just like JavaServer Pages (JSP). JSF is an XML document representing formal components in a logical tree, unlike JSP, which is just an HTML page with server-side capabilities. JSF components are supported by Java objects, separate from HTML, and possess all of Java's features, including the ability to access distant APIs and databases


Now let's have a look at the working of the JSF Life Cycle.

JSF Life Cycle mainly works on three sides which are given below.

1. Server-side

The JavaServer Faces (JSF) application code you create executes on the server, but components may also contain JavaScript code that executes on the client. In contrast, client-side frameworks like Angular allow you to write JavaScript or Typescript code that executes in the web browser's client-side environment.

Although it is a server-side framework, this does not preclude the usage of client-side code in JSF applications. JSF components can handle AJAX, communication, the team's client-side and server-side code, JavaScript, and JSF.

2. Component side

Building web projects with JavaServer Faces is quicker and simpler since the building blocks used are reusable, self-contained components.

JavaServer Faces' most intriguing feature, which distinguishes it from action-based web application frameworks like Spring Web MVC (Model-View-Controller), Struts, or ordinary Servlets and JavaServer pages, is that it is component-based. This indicates that the foundational elements of your JSF application are reusable and standalone components.

3. Web UI Framework

The user interface component of web applications is the emphasis of JavaServer Faces.

 

                 

        Fig. JavaServer Faces Standard Request-Response Lifecycle

JSF Life Cycle

The two key stages of the life cycle are

  •  Execute 
  •  Render

Additional subphases inside the Execute phase support the complex component tree. This structure necessitates the orderly propagation of component data to beans, the conversion and validation of component data, and the handling of component events.

Execute Phase

The application view is constructed or rebuilt when the first request is performed during the execution phase. Other actions are carried out for subsequent requests, such as applying request parameter values, converting component values, validating component values, updating managed beans with component values, and invoking application logic.

The Execute phase is divided into six sub-phase, which are discussed below:

1. Restore View Phase

The JavaServer Faces implementation starts the Restore View phase when a request for a JavaServer Faces page is made, typically by action, like when a link or button component is clicked.

The JavaServer Faces implementation constructs the page's view during this stage, attaches event handlers and validators to view components, and saves the view in the FacesContext instance, which has all the data required to handle a single request. The FacesContext object is accessible to all the application's parts, event handlers, converters, and validators.

2. Apply Request Values Phase

Each component in the tree pulls its new value from the request parameters by utilizing its decode (processDecodes()) method after the component tree has been restored during a postback request. Each component then stores the value locally.

The JavaServer Faces implementation moves directly to the Render Response stage if any decoding methods or event listeners have invoked the renderResponse method on the current FacesContext instance.

The JavaServer Faces implementation sends any events queued during this stage to interested listeners.

The components are now set to their new values, and events and messages have been queued.

3. Process Validations Phase

JSF processes all validators registered on the component tree during this stage. It looks at the attribute rules for the component for validation and contrasts them with the local value kept for the component. The life cycle moves on to the render response phase and displays the same page again with the error message if the local value is wrong, and JSF adds an error message to the FacesContext object.

4. Update model values

In this phase, Following a valid data check, the JSF traverses the component tree and sets the necessary server-side object properties to the components' local values. JSF will update the bean properties that correspond to the input component's value attribute.

The render response phase of JSF begins if any updateModels methods call renderResponse on the current FacesContext instance.

5. Invoke application

In this phase, Any application-level actions, such as submitting a form or linking to another page, are handled by the JavaServer Faces implementation.

The JavaServer Faces implementation finally gives the Render Response phase control.

6. Render Response Phase

In this stage, JavaServer Faces creates the view and gives the proper resource the authority to render the pages.

If this is the first request, the component tree will be expanded to include the page's representations of the components. The components are already added to the tree and do not need to be added again if this is not the first request.

The original page is rendered again during this step if the request is a postback and errors were discovered during the Apply Request Values phase, Process Validations phase, or Update Model Values phase.

Render

The requested view is rendered in response to the client browser during this stage. View rendering is a procedure where HTML or XHTML is produced as the output. Therefore, a person can view it in a browser.

Frequently Asked Questions

What are Facelets in JSF?

Facelets tags are special tags JSF offers to build a common layout for a web application.

What are controllers in JSF Application?

"FacesServlet" is the central controller for the JSF application.

Which tag does JSF use to upload a file to a website?

In JSF, the tag is used to upload a file to the website. JSF utilises the HTML file type element. The file will receive this tag to receive input.

What are the various bean scopes available in JSF?

Different bean scopes, such as @RequestScoped, @NoneScoped, @ViewScoped, @SessionScoped, @ApplicationScoped, and @CustomScoped, are available in JSF.

What are the two phases of the life cycle?

  • Execute 
  • Render

Conclusion

Finally, you've concluded this article. 

Congratulations!! You learned about  JSF Life Cycle and how it works in this blog. You studied the working of the life cycle and its different phases.

After reading these interview questions, are you eager to read more articles on the subject of JSF? Don't worry; Coding Ninjas will take care of everything.

Check out the awesome content on the Coding Ninjas Website:

JSF 

JSF architecture

JSF Application Events

Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass