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 Java, JDK, java.util, and java platform. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System 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 problems, interview 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!