Managed Beans
Start course
1h 9m

In this course, we will learn the concepts of Java EE 7 with a focus on JavaServer Faces.

Learning Objectives

  • What JavaServer Faces are

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello dear friends. In this video, we will examine the Managed Bean and scope annotations. Let's begin. Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java Bean managed by the JSF framework. Managed Bean contains the getter and setter methods, business logic, and even a backing Bean. Managed Beans work as model for UI components and can be accessed from a JSF page. In JSF 1.2, a Managed Bean had to be registered in the JSF configuration file such as faces-config.xml. From JSF 2.0 onwards, Managed Beans can be easily registered using annotations. This approach keeps Beans and its registration at one place. Hence, it becomes easier to manage. Following our common functions that Managed Bean methods perform; validating a components data, handling an event fired by a component, performing processing to determine the next page to which the application must navigate. Now, let's use Managed Beans.

First, I will start by creating a Java class. Add a new Java class and name it Author. Now, let's define some properties; name, surname, maybe age. Done. Generate getters and setters. Our class is ready. As I previously mentioned, there are two methods to convert a class to Managed Beans. First, xml. In web INF folder, open faces-onfig.xml. Go to the source. Now, in this area, we can define all Managed Beans or scope annotations. For the time being, let's focus on Managed Beans. So, let's first open a Managed Beans tag. Now, we need a Managed Beans name tag. This will hold our Managed Beans name that we will use in XHTML pages. Now we will add Managed Beans class. Set class name to Author. Now, Managed Beans is ready. Let's use it in our XHTML file. First, let's add a form. I will explain this in the next video but for now I will only add. Now add input.

As you know, in html input has a value property. Now with Managed Bean, we will set input value to the Author name. First, we need a sharp. Now add curly braces. Into the braces, we will call our Managed Beans Looks very easy. Since we'll be looking at the most commonly used form elements with Managed Beans in the next video, I decided not to provide any code examples at this moment. Now, let's use the second approach. Using annotations to change our class into a Managed Bean. Delete these xml codes. Now open author class. First, we have to import javax.faces.bean.managedbeanlibrary. Now, let's define annotation. We will set the annotation name to Managed Bean. If you use this annotation, the name of Managed Beans will be the class name. However, you can change your name if you wish. For example, new name managed. Now go to the XHTML file. Look, there is an error because we don't have any Managed Beans named Author. If we changed with newmanaged, look, there is no error.

Managed Bean is lazy by default, it means Bean is instantiated only when a request is made from the application. You can force a Bean to be instantiated and placed in the application scope as soon as the application is started. For this, we only need an eager. If we add eager=true into the Managed Beans, this will be enough for us. I hope you now have a better understanding of Managed Beans. Let's start looking at scope annotations. Scope annotation set the scope into which the Managed Bean will be placed. If the scope is not specified, then Bean will default to request scope. There are six scope annotations: Request scoped, none scoped, view scoped, session scoped, application scoped. In request scoped, the Bean lives as long as the http request response lives. It gets created upon an http request and gets destroyed when the http response associated with the http request is finished.

In none scoped, Bean lives as long as a single EL evaluation. It gets created upon an EL evaluation and gets destroyed immediately after the EL evaluation. In view scoped, Bean lives as long as the user is interacting with the same JSF view in the browser window tab. It gets created upon an http request and gets destroyed once the user post backs to a different view. In session scoped, Bean lives as long as the http session lives. It gets created upon the first http request involving this Bean in the session and gets destroyed when the http session is invalidated. In application scoped, the Bean lives as long as the web application lives. It gets created upon the first http request involving this Bean in the application or when the web application starts up and the eager=true attribute is set in at Managed Bean and gets destroyed when the web application shuts down. in the session scoped, the Bean lives as long as the Bean's entry in the custom map, which is created for the scope lives. Using scope annotations is similar to using Managed Beans. First, we need to import javax.faces.bean.RequestScoped and then we will only add the selected scoped like SessionScoped. Done. That's all for now. In the next video, we will examine UI components as form elements in JSF. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics