JSF ManagedBean is a java class that can be accessed by the JSF page for retrieving the values of the UI fields.
JSF ManagedBean
JSF Managed Bean can be created using @ManagedBean
annotation. If a name attribute is not specified after the @ManagedBean
annotation, it indicates that the name of the managed bean is the same as the class name. If the name is specified then it is a managed bean with the name specified in the attribute.
There are other annotations specifying the scope attributes which can be used with the managed bean.
@SessionScoped
: Indicates that the bean is valid for the whole session
@RequestScoped
: The bean is valid for the http request.
@ApplicationScoped
: Bean is valid as long as the web application is valid.
Let’s now create a managed bean named Car.java as shown below.
package com.journaldev.jsf.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class Car {
private String cname = "Santro";
private String color = "blue";
private String Id = "S1";
private String model = "S-800";
private String regno = "S4567";
private String description;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
}
In the above code, since we have not used any name attribute after @ManagedBean
annotation, a managed bean with the name Car is created. @SessionScope
is used to indicate that the bean is valid for the whole session. The Car bean basically contains the fields of the car like color, id, description, model and regno along with the getter and setter methods for these.
Now create viewdetails.xhtml
which is the view for the details of the car.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:h="https://xmlns.jcp.org/jsf/html">
<h:head>
<title>Car Details</title>
</h:head>
<h:body>
Car Id:#{car.id}
<br>
<br>
Car Name:#{car.cname}
<br>
<br>
Car color:#{car.color}
<br>
<br>
Car Model:#{car.model}
<br>
<br>
Car Registration Number:#{car.regno}
</h:body>
</html>
Now run the application and it should produce the following output in the browser.
Injecting JSF Managed Bean
The @ManagedProperty
annotation enables us to inject a managed bean into another managed bean. Let’s look into an example of how to inject the managed bean.
Create the managed bean named Car_gen.java as below.
package com.journaldev.jsf.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "car_gen", eager = true)
@RequestScoped
public class Car_gen {
@ManagedProperty(value = "#{alto_car}")
private Alto alto;
private String description;
public Car_gen() {
}
public String getDescription() {
description = alto.getDescription();
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Alto getAlto() {
return alto;
}
public void setAlto(Alto alto) {
this.alto = alto;
}
}
Here the bean named alto_car is injected with the @ManagedProperty
annotation. In the getter method we call the getDescription method of alto to fetch the value from the alto managed bean.
The #{alto_car} indicates that the managed bean named alto_car is injected as we have specified in the name attribute of @ManagedBean
annotation in Alto.java
class.
The “eager=true” indicates that the managed bean is created before it is requested for the first time.
Next create the bean named Alto.java as shown below.
package com.journaldev.jsf.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name = "alto_car", eager = true)
@RequestScoped
public class Alto {
private String description = "Alto has power windows";
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
This managed bean contains the description field with the value initialised and has getter and setter methods.
Now we create a JSF page named car_gen.xhtml
to bind this.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:h="https://java.sun.com/jsf/html">
<h:head>
<title>Alto Description</title>
</h:head>
<h:body>
#{car_gen.description}
</h:body>
</html>
We fetch the description value from the car_gen
bean which further fetches the value from alto bean. This is possible because the Alto bean is being injected into the generic car_gen
bean.
Upon running the code, we can see the JSF page rendering the description of alto car in the browser.
The final project structure is shown below.
You can download the project from below link and play around with it to learn more.