Managed Beans
Use
Managed beans provide the underlying logic of JSF applications - for example, when you want to display information about customers, the managed beans obtain and process the database data.
The user interface is bound to the managed bean(s) via the bean properties. A bean property is a normal variable in the bean source code with a setter and/or getter method. If a variable is to be read-only from the UI, create a getter method. If it is to be write only, create a setter method.
A managed bean is a pure old Java class (POJO). It does not need to implement specific interfaces or extend specific classes.
These code samples would create a simple HTML form where the user can submit data that will be set as values of the propA and propB properties.
For example, the com.sap.engine.examples.backingbeans.DepartmentListBean enables access to all departments available in the company database. It provides two properties to the JSF user interface:
-
allDepartments - represents the collective property of all departments
-
selectedDepartments - represesnts the collective property of the currently selected departments
Because these properties are read-only, they only have getter methods.
package com.sap.engine.examples.backingbeans;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sap.engine.examples.ejb3.edm.beans.entity.Department;
import com.sap.examples.hrservices.HRServices;
public class DepartmentListBean {
private HRServices hrServices;
private List<DepartmentBean> allDepartments;
private List<DepartmentBean> selectedDepartments =
new ArrayList<DepartmentBean>();
public DepartmentListBean() {
try {
InitialContext ctx = new InitialContext();
hrServices = (HRServices)
ctx.lookup(HRServices.class.getName());
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
public List<DepartmentBean> getAllDepartments() {
if (allDepartments == null) {
readDepartments();
}
return allDepartments;
}
public List<DepartmentBean> getSelectedDepartments() {
return selectedDepartments;
}
. . .
}