Show TOC

Background documentationManaged Beans Locate this document in the navigation structure

 

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.

Example Example

For example, it may look like this:

  1. public class MyManagedBean {
    
      private String propA;
      private String propB;
      //. . .
      public String getPropA() {
        return propA;
      }
    
      public void setPropA(String _propA) {
        this.propA = _propA;
      }
    
      public String getPropB() {
        return propB;
      }
    
      public void setPropB(String _propB) {
        this.propB = _propB;
      }
      //. . .
    }
    
End of the code.

Source Code Source Code

In the JSF source code, the binding to the propA and propB properties can look like this:

  1. <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    
    <f:view>
      <h:form>
        <h:panelGrid columns="1">
          <h:outputText value="Submit Data"/>
          <h:outputText value="Property A:"/>
          <h:inputText value="#{myBean.propA}"/>
          <h:outputText value="Property B:"/>
          <h:inputText value="#{myBean.propB}"/>
          <h:commandButton value="Set Properties" action="set_props"/>
        </h:panelGrid>
      </h:form>
    </f:view>
    
    
End of the code.

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.

Syntax Syntax

  1. 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;
      }
    
     . . .
    }
    
End of the code.