How do I use backing beans?

Backing beans are Java beans which can be used to better manage the behavior of components on a JSP page. In practice, you can create different backing beans for one or more components on a page, however, it can be useful to have a one-to-one relationship between the web pages and backing beans in your application. For example, if your web page consists of logon.jsp, contents.jsp, and view.jsp, then create three backing beans to manage the behavior of the components on these pages: Logon.java, Contents.java, and View.java.

Consider the case of a web page called logon.jsp, which contains a Logon component. This page is used to provide a form that allows users to attempt to log on to BusinessObjects Enterprise.

When the logon form is submitted, an event handling method (referenced by the action attribute of the Logon component in the page) must determine the success or failure of the logon, and then return appropriate navigation strings that can be interpreted by the faces-config.xml file (see How do I define the navigation flow of my application?). In this case, a backing bean, called Logon.java, is a good location to create these methods in and handle the behavior of the logon.jsp page.

To create a logon backing bean
  1. Create the Logon.java source class and compile into a Logon.class file.
  2. Declare the bean in the faces-config.xml file of your application:
  3. <managed-bean>

        <managed-bean-name>Logon</managed-bean-name>

        <managed-bean-class>pagecode.Logon</managed-bean-class>

        <managed-bean-scope>request</managed-bean-scope>

    </managed-bean>

  4. Deploy your Logon.class file with your web application.
  5. Note:    This example assumes that the compiled Java bean (Logon.class) will be deployed as part of the pagecode package in a JAR file contained in the WEB-INF\lib directory of your application. Alternatively, you can store the compiled Logon.class file in a WEB-INF\classes\pagecode directory.

Here is an example of a backing bean for a simple logon page.

Example

package pagecode;

    

import com.businessobjects.jsf.sdk.model.IIdentity;

import javax.faces.context.FacesContext;

    

public class Logon

{

    protected IIdentity identity;

    

    public IIdentity getIdentity()

    {

        if (identity == null) {

            identity = (IIdentity) FacesContext.getCurrentInstance().getApplication().createValueBinding("#{identity}").getValue(FacesContext.getCurrentInstance());

        }

        return identity;

    }

    

    public void setIdentity(IIdentity identity)

    {

        this.identity = identity;

    }

    

    public String logonAction()

    {

        identity = getIdentity();

        if (identity == null)

            return "logon_failure";

        if(!identity.isLoggedOn())

            return "logon_failure";

        return "logon_success";

    }

}



Business Objects
http://www.businessobjects.com/
Support services
http://www.businessobjects.com/services/support/