Friday, May 21, 2010

Filling SelectOneChoice From Backingbean (ADF)

One of the things I like about developing with JDeveloper and ADF is that you can do an awful lot by just dragging components and data controls and dropping them into a web form. Configure a few properties to control how the component works and looks, and you're done. But sooner or later you do have to write a little Java code.

So, awhile back I needed to display data in SelectOneChoice component after fetching values from the database in a bean using Application module implementation class.

The following is what I did:

1. In the backingbean, define a variable that will hold the selected Value ( i.e. The value which you will select from the selectOneChoice component ) [e.g public String selectedValue;] and make its setter and getter methods.

2. In the backingbean, define an array of selectItem that will fill the selectOneChoice (e.g. public SelectItem[] elements = null; ) and make its getter and setter method.

3. In the getter method of selectItem, write this code :


  1. BindingContainer bindings = getBindings();
  2. DCIteratorBinding iterator = (DCIteratorBinding)bindings.get("fetchAllServiceProviderTypesIterator");
  3. int length = iterator.getRangeSize() > 0 ? iterator.getRangeSize() : new Long(iterator.getEstimatedRowCount()).intValue();
  4. elements = new SelectItem[length];
  5. SelectItem item = null;
  6. for (int i = 0; i < length; i++) {
  7. Row row = iterator.getRowAtRangeIndex(i);
  8. if (row != null) {
  9. item = new SelectItem();
  10. item.setValue(row.getAttribute("code").toString());
  11. item.setLabel(row.getAttribute("name").toString());
  12. }
  13. elements[i] = item;
  14. }
  15. return elements;

Where : fetchAllServiceProviderTypesIterator is the Iterator which its data will fill the selectOneChoice (You must add this iterator in the pageDef).

Now, your final backingbean should look something like this :


  1. public class ServiceProviderBacking {
  2. /** An array of selectItem that will fill the ServiceProviderType selectOneChoice */
  3. public SelectItem[] elements = null;
  4. /** Variable to hold the selected Service Provide Type Value */
  5. public String selectedValue;
  6. private BindingContainer bindings;
  7. public void setElements(SelectItem[] elements) {
  8. this.elements = elements;
  9. }
  10. /**
  11. * Populate the selectObeChoice with the ServiceProviderType values
  12. *
  13. * @return
  14. */
  15. public SelectItem[] getElements() {
  16. BindingContainer bindings = getBindings();
  17. DCIteratorBinding iterator = (DCIteratorBinding)bindings.get("fetchAllServiceProviderTypesIterator");
  18. int length = iterator.getRangeSize() > 0 ? iterator.getRangeSize() : new Long(iterator.getEstimatedRowCount()).intValue();
  19. elements = new SelectItem[length];
  20. SelectItem item = null;
  21. for (int i = 0; i < length; i++) {
  22. Row row = iterator.getRowAtRangeIndex(i);
  23. if (row != null) {
  24. item = new SelectItem();
  25. item.setValue(row.getAttribute("code").toString());
  26. item.setLabel(row.getAttribute("name").toString());
  27. }
  28. elements[i] = item;
  29. }
  30. return elements;
  31. }
  32. public void setSelectedValue(String selectedValue) {
  33. this.selectedValue = selectedValue;
  34. }
  35. public String getSelectedValue() {
  36. return selectedValue;
  37. }
  38. /**
  39. * Get the page bindings
  40. *
  41. * @return
  42. */
  43. public BindingContainer getBindings() {
  44. if (this.bindings == null) {
  45. FacesContext fc = FacesContext.getCurrentInstance();
  46. this.bindings =
  47. (BindingContainer)fc.getApplication().evaluateExpressionGet(fc,
  48. "#{bindings}",
  49. BindingContainer.class);
  50. }
  51. return this.bindings;
  52. }
  53. }

4. In your page, drag a selectOneChoice component and set its value to :

#{yourBean.selectedValue}

5. Insert f:selectItems inside the af:selectOneChoice and set its value to:

#{yourBean.elements}

That's all there is to it.

By the way, while what I am showing you DOES work, it may not be the optimal solution - if a more experienced ADF developer wants to chime in with a better way please do.

1 comment: