Table of contents
1.
Introduction
2.
JSF Managed Beans
3.
Using JSF Managed Beans
3.1.
By setting up JSF Managed Bean into an XML file
3.2.
Using annotated text for JSF Managed Beans 
4.
Scopes for JSF Managed bean class
5.
Coded Examples of JSF Managed Beans
6.
Frequently Asked Questions
6.1.
What is a JSF tag?
6.2.
What is the JSF form?
6.3.
What is a facet tag in JSF?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

JSF Managed Beans

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

Introduction

JavaServer Faces or JSF is a new Java standard technology for building component-based and event-oriented web interfaces. Similar to JavaServer Pages (JSP), JSF allows server-side access to data and logic.

In this article, we will understand a very vital concept in JSF for web development. We will learn about JSF Managed beans, various bean classes and annotations of JSF managed beans and we will also look at various coded examples of JSF-managed beans.

JSF Managed Beans

Before going further first, we need to know the basics of JSF-managed beans; what exactly is it? So, the normal Java Bean class registered with JSF is the Managed Bean. It’s a lightweight Java Bean that is registered with the framework, to contain the Backend data, and that may contain business logic. Managed beans serve as the UI component's model. JSF page provides access to Managed Bean.

The following are typical tasks carried out by JSF-managed bean methods:

1. Checking the data in a component

2. Processing to determine the next page the program needs to browse after handling an event fired by a component

3. It serves as a model for the JFS Framework as well.

Using JSF Managed Beans

These are some ways that you can use JSF Managed Beans.

1. By setting up an XML file.

2. Using annotated text.

By setting up JSF Managed Bean into an XML file

This is an older method of setting up beans in XML files. In order to use this method, an XML file called faces-config.xml must be created. To set up the bean, JSF offers the <managed-bean> tag.

We are listing the bean name, bean class, and bean scope in the sample below. As a result, it may be usable in the project.

<managed-bean>
   <managed-bean-name>helloWorld</managed-bean-name>
   <managed-bean-class>com.codingninjas.test.HelloWorld</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean> 

<managed-bean>
   <managed-bean-name>message</managed-bean-name>
   <managed-bean-class>com.codingninjas.test.Message</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Using annotated text for JSF Managed Beans 

By default, managed beans are lazy. It indicates that a bean is only created when an application requests it.

import javax.faces.bean.RequestScoped;
import javax.faces.bean.ManagedBean;  
@RequestScoped  // Using Scope annotation  
@ManagedBean    // Using ManagedBean annotation  
public class Vehicle {  
    private String name; 
    public String model; 
    public String getName() {  
        return name;  
    }   
 public String getModel() {  
        return model;  
    }  
    public void setName(String name) {  
     this.name = name;  
}   
 public void setModel(String model) {  
     this.model = model;  
} 
} 

Scopes for JSF Managed bean class

Coded Examples of JSF Managed Beans

sayHello.java

//importing modules
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
//setting eager value to true
@ManagedBean(name = "sayHello", eager = true)
@RequestScoped
//declaring sayHello class
public class sayHello {
   private String message = "Hello from Coding Ninjas!";
   public String getHello() {
      return message;
   }
 public void setHello(String message) {
      this.message = message;
   }
}
You can also try this code with Online Java Compiler
Run Code

 

HelloWorld.java

//importing modules
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "helloWorld", eager = true)
@RequestScoped

// declaring HelloWorld Class
public class HelloWorld {
   @ManagedProperty(value = "#{message}")
   //setting private Message and String message
   private Message messageBean;
   private String key;
   
// printing the message
   public HelloWorld() {
      System.out.println("Hello From Coding Ninjas!");
   }
   
// declaring getMessage , this will return message
   public String getMessage() {
 if (messageBean != null) {
         key = messageBean.getMessage();
      }
      return key;
   }
   
// setting the message
   public void setMessageBean(Message hello) {
      this.messageBean = hello;
   }
}
You can also try this code with Online Java Compiler
Run Code

 

hello.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   <body style="color:red;text-align:center;font-size:32px">
      #{helloWorld.message}
   </body>
</html>

 

Output:

Frequently Asked Questions

What is a JSF tag?

JSF provides a standard HTML tag library. These tags render into corresponding HTML output. For these tags, we need to use some namespaces of URI in the HTML node.

What is the JSF form?

The <h:form> tag in JSF represents an input form with child components that may contain data presented to the user or submitted with the form. It might also include HTML markup laying out the components on the page.

What is a facet tag in JSF?

A facet tag in a project is a specific unit of functionality you can add to a project when you require that functionality. When you add a project facet to a project, it can add nature, classpath entries, builders, and resources to that project, depending on the nature and characteristics.

Conclusion

In this article, we have extensively discussed JSF Managed Beans, the ways to use them, the scopes of JSF-managed beans, and finally, the coded example.

After reading about JSF Managed Beans, are you not feeling excited to read/explore more articles on the topic of  Java and JSF? Don't worry; Coding Ninjas has you covered. To learn, see the basics of JavaJDKjava.util, and java platformRefer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass