Using a Bean Data Control in Oracle ADF 11g

Hi All, here is small tutorial in Oracle ADF technology, how to use Data Controls. Basically there are three different types of data controls in ADF

  • Bean Data Control
  • URL service Data Control
  • PlaceHolder Data Control

In this tutorial i am going to elaborate on Bean Data control.this tutorial shows a simple databound application using JavaBeans and Oracle ADF Faces.

Step 1: Create a New Application and Project

From the main menu, choose File > New. In the New Gallery, expand the General category and select Applications. Then in the Items list, select Custom Application and click OK.

new_project

2. To follow along with the example, enter DataBoundADF as the application name and click Next.
3. Enter Model as the project na
4. Click Finish.

The Projects panel in the Application Navigator should look like this:
Model

Step 2: Create a Simple JavaBean Class

In the Application Navigator, right-click the project you just created and choose New > General > Java Class, then click OK.

new_javaclass

2.In the Create Java Class dialog, enter Contact as the class name, and com.spark.adf.tutorial as the package name. Accept the default values and click OK.

3.In the source editor, add code to create a simple JavaBean class.

package com.spark.adf.tutorial;

/**
 * @author Pavan Kumar Mantha
 */
public class Contact {

    private String name;
    private String email;

    public Contact() {
        super();
    }

    public Contact(String name, String email) {
        this.name = name;
        this.email = email;
    }

    /**
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     */
    public String getName() {
        return name;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }
}

Click saveall icon Save All to save your work.

In the Application Navigator, you should see Contact.java in the com.spark.adf.tutorial package in the Application Sources folder.

Step 3: Create a Service Class

In the Application Navigator, right-click the Model project and choose New > General > Java Class, then click OK.

In the Create Java Class dialog, enter AddressBook as the class name. Accept the package name as com.spark.adf.tutorial.service, and the remaining default values, then click OK.

3.In the source editor, add code to create a collection Java class.
Delete all the generated code and replace with the following code:

package com.spark.adf.tutorial.service;


import com.spark.adf.tutorial.Contact;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author Pavan Kumar Mantha
 */
public class AddressBook {

    List<Contact> contacts = new ArrayList<Contact>();

    public AddressBook() {
        super();
        contacts.add(new Contact("Steve", "steve@acme.org"));
        contacts.add(new Contact("Charles", "cyoung@acme.org"));
        contacts.add(new Contact("Karl", "kheint@acme.org"));
        contacts.add(new Contact("Mik", "mik_meir@acme.org"));
        contacts.add(new Contact("Yvonne", "yvonne_yvonne@acme.org"));
        contacts.add(new Contact("Sung", "superstar001@acme.org"));
        contacts.add(new Contact("Shailyn", "spatellina@acme.org"));
        contacts.add(new Contact("John", "jjb@acme.org"));
        contacts.add(new Contact("Ricky", "rmartz@acme.org"));
        contacts.add(new Contact("Shaoling", "shaoling@acme.org"));
        contacts.add(new Contact("Olga", "olga077@acme.org"));
        contacts.add(new Contact("Ron", "regert@acme.org"));
        contacts.add(new Contact("Juan", "jperen@acme.org"));
        contacts.add(new Contact("Uday", "udaykum@acme.org"));
        contacts.add(new Contact("Amin", "amin@acme.org"));
        contacts.add(new Contact("Sati", "sparek@acme.org"));
        contacts.add(new Contact("Kal", "kalyane.kushnane@acme.org"));
        contacts.add(new Contact("Prakash", "prakash01@acme.org"));
    }

    public List<Contact> findAllContacts() {
        return contacts;
    }

    public List<Contact> findContactByName(String name) {

        String namePattern = ".*" + (name != null ? name.toUpperCase() : "") + ".*";
        List<Contact> l_contacts = new ArrayList<Contact>();
        for (Contact contact : contacts) {
            if (Pattern.matches(namePattern, contact.getName().toUpperCase())) {
                l_contacts.add(contact);
            }
        }
        return l_contacts;
    }
}

4. Click saveall icon Save All to save your work.

Step 4: Create a Data Control for the Service Class
1. In the Application Navigator, right-click AddressBook.java and choose Create Data Control.

DataControls

2.In the Bean Data Control Interface Chooser dialog, click OK without selecting an option.

JDeveloper adds the data control definition file (DataControls.dcx ) to the project, and opens the file in the overview editor.
dcx_overview

3.In the Application Navigator, expand the Data Controls panel, then expand AddressBook.
DataControls

Step 5: Create a JSF Page

1. From the main menu, choose File > New > General > Projects > Custom Project, then click OK.

new_genproj

2. Enter View as the project name. Then select ADF Faces from the Available list and shuttle icon shuttle it to the Selected list.

3. Click Finish.
You should see the View project in the Application Navigator.

4. In the Application Navigator, right-click the View project and choose New > Web Tier > JSF/Facelets > Page, then click OK

5. In the Create JSF Page dialog, enter ContactList.jsf as the file name. Make sure Facelets is the selected document type.

6. On the Page Layout page, select Blank Page. On the Managed Bean page, select Do Not Automatically Expose UI Components in a Managed Bean.

7. Click OK.
By default JDeveloper displays the new JSF Facelets page in the visual editor.

8. In the Component Palette, ADF Faces page, Layout panel, drag borderlayout icon Panel Stretch Layout and drop it on the blank page in the visual editor.

When you drag the component to the visual editor, you should see a target rectangle with the name Form on the page; this means the component you are dragging will be inserted inside that target component.

9. click save all icon to save the work.

Step 6: Bind an ADF Faces Table Component to the Service Bean
1. In the Data Controls panel, expand AddressBook, then expand findAllContacts().

dcpalette3

2. Click and drag Contact to the center facet on the page in the visual editor. From the Create context menu, choose Table > ADF Read-only Table.

createcontextmenu

3. In the Edit Table Columns dialog, select Enable Sorting.edittablecols

4. Accept the default values and click OK.
The page in the visual editor should look similar to this:

tableineditor

5.In the Application Navigator, right-click ContactList.jsf and choose Run.

If the Create Default Domain dialog displays, enter the default password, for example weblogic1, in the Password and Confirm Password fields, then click OK.

The page in the browser should look similar to this:
tableinbrowser1

I will teach how to bind “Bind to a Parameterized Method and a Parameter” in my next post.
Happy ADF data binding. 🙂