The course is part of this learning path
This course explores the fundamentals of web applications, JavaServer Faces, and then walks you through a variety of projects that will give you a practical demonstration of how to create a web application and how to use various components of JavaServer Faces.
- Understand the fundamentals of web applications
- Learn about JavaServer Faces, and how they can be used in your applications
This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE.
Basic knowledge of Java programming.
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 are common functions that managed bean methods perform: Validating a component's 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.config.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 Author.name. 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.ManagedBean library. Now, let's define annotation. We will set the annotation name to ManagedBean. 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 beans entry in the custom map, which is created for this 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 session scoped. 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.
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.