How to programmatically populate a selectOneChoice component using data stored on a Coherence cache


The purpose of this is to show how both the web application and database performances can be improved by using Oracle Coherence as the jspx page does not need to query the database. This document contains a simple example (how-to steps and downloadable sample workspace) about how to populate ADF components based on a Coherence cache. The JDeveloper 10.1.3.5 workspace can be downloaded from here and does not need a database connection to run, but coherence.jar must be imported as explained on step 4.

 

 

1. Create a new web application with JSF and ADF BC.

 

 

2. Once this new application is created go to the ViewController project and create a new jspx page. Expose the UI components in a managed bean.

 

 

3. Place an af:selectOneChoice component on the jspx page.

 

 

4. Import coherence.jar to the ViewController project. Right-click on its icon, select "Project Properties", select "Libraries" at the left menu, click on the "Add Jar/Directory ..." button, browse to your coherence.jar file and click "Select".

 

 

5. A way to make sure the data contained in the Coherence cache is populated when the page loads is to use the backing bean class constructor to call the populating method (the OnPageLoad-PagePhaseListener functionality is useful in pages of applications that use a database connection to run).

 

 

6. Source code for the jspx page and the managed bean used on this example (below):

 

 

TestPage.jspx

 

<?xml version='1.0' encoding='windows-1252'?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:f="http://java.sun.com/jsf/core"

          xmlns:af="http://xmlns.oracle.com/adf/faces"

          xmlns:afh="http://xmlns.oracle.com/adf/faces/html">

  <jsp:output omit-xml-declaration="true" doctype-root-element="HTML"

              doctype-system="http://www.w3.org/TR/html4/loose.dtd"

              doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>

  <jsp:directive.page contentType="text/html;charset=windows-1252"/>

  <f:view>

    <afh:html>

      <afh:head title="TestPage">

        <meta http-equiv="Content-Type"

              content="text/html; charset=windows-1252"/>

      </afh:head>

      <afh:body>

        <h:form>

          <af:selectOneChoice label="Choices:"

                              binding="#{backing_TestPage.selectOneChoice1}"

                              id="selectOneChoice1">

            <f:selectItems value="#{backing_TestPage.listOfItems}"

                           id="selectItems1"/>

          </af:selectOneChoice>

        </h:form>

      </afh:body>

    </afh:html>

  </f:view>

  <!--oracle-jdev-comment:auto-binding-backing-bean-name:backing_TestPage-->

</jsp:root>

 

 

TestBean.java

 

package view.backing;

 

import com.tangosol.net.CacheFactory;

import com.tangosol.net.NamedCache;

import com.tangosol.util.Filter;

import com.tangosol.util.extractor.IdentityExtractor;

import com.tangosol.util.filter.LikeFilter;

 

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

import javax.faces.model.SelectItem;

 

import oracle.adf.view.faces.component.core.input.CoreSelectOneChoice;

 

public class TestBean {

 

    private CoreSelectOneChoice selectOneChoice1;

   

    private List listOfItems = new ArrayList();

   

    public TestBean() // Constructor

    {     

        populateComponent();     

    }

   

    public void populateComponent() {

        SelectItem si = new SelectItem();     

        CacheFactory.ensureCluster();                     

        NamedCache c = CacheFactory.getCache("mycache");

        Filter query = new LikeFilter(IdentityExtractor.INSTANCE, "%", '\\', true);

        // Perform query, return keys of entries that match. In this case the query will return all the entries in the cache.

        Set keys = c.keySet(query);

        Set hashSet = new HashSet();

        for (Iterator i = keys.iterator(); i.hasNext();) {

          hashSet.add(i.next());

        }

        Map map = c.getAll(hashSet);

        for (Iterator ie = map.entrySet().iterator(); ie.hasNext();) {

          Map.Entry e = (Map.Entry) ie.next();

          System.out.println("key: "+e.getKey() + ", value: "+e.getValue());

          si = new SelectItem();

          si.setLabel(e.getValue().toString());

          si.setValue(e.getValue().toString());

          listOfItems.add(si);

        }

        CacheFactory.shutdown();

    }

 

    public void setSelectOneChoice1(CoreSelectOneChoice selectOneChoice1) {

        this.selectOneChoice1 = selectOneChoice1;

    }

 

    public CoreSelectOneChoice getSelectOneChoice1() {

        return selectOneChoice1;

    }

   

    public void setListOfItems(List listOfItems)

    {

      this.listOfItems = listOfItems;

    }

 

    public List getListOfItems()

    {

      return listOfItems;

    }

}

 

 

7. Before running the page, make sure the Coherence cache contains data. In this example, to make it easier, we do it directly through the Coherence console. To do this run cache-server.cmd (Windows) or cache-server.sh (Linux/Unix) to make sure there is at least one storage enabled node, and then coherence.cmd or coherence.sh to put data in the cache. In this example we will use a Coherence cache named "mycache" and put 3 entries on it: "Red", "Blue" and "Yellow", as can be seen at the image below.


consolacoherence.jpg

 

 

8. Before running the page, set it to run as a client node (storage disabled) by adding the following Java option: -Dtangosol.coherence.distributed.localstorage=false


javaoptions.jpg


 

9. Run the jspx page.


browser.jpg


 

The JDeveloper 10.1.3.5 workspace can be downloaded from here. It does not need a database connection to run, but coherence.jar must be imported as explained on step 4.

 

To learn how to populate a Coherence cache based on a database query, please see http://coherence.oracle.com/display/COH33UG/Bulk+Loading+and+Processing+with+Coherence


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About


Principal Technical Support Engineer in the Exalogic Enterprise Support Team (EEST).

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today