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 :
BindingContainer bindings = getBindings();DCIteratorBinding iterator = (DCIteratorBinding)bindings.get("fetchAllServiceProviderTypesIterator");int length = iterator.getRangeSize() > 0 ? iterator.getRangeSize() : new Long(iterator.getEstimatedRowCount()).intValue();elements = new SelectItem[length];SelectItem item = null;for (int i = 0; i < length; i++) {Row row = iterator.getRowAtRangeIndex(i);if (row != null) {item = new SelectItem();item.setValue(row.getAttribute("code").toString());item.setLabel(row.getAttribute("name").toString());}elements[i] = item;}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 :
public class ServiceProviderBacking {/** An array of selectItem that will fill the ServiceProviderType selectOneChoice */public SelectItem[] elements = null;/** Variable to hold the selected Service Provide Type Value */public String selectedValue;private BindingContainer bindings;public void setElements(SelectItem[] elements) {this.elements = elements;}/*** Populate the selectObeChoice with the ServiceProviderType values** @return*/public SelectItem[] getElements() {BindingContainer bindings = getBindings();DCIteratorBinding iterator = (DCIteratorBinding)bindings.get("fetchAllServiceProviderTypesIterator");int length = iterator.getRangeSize() > 0 ? iterator.getRangeSize() : new Long(iterator.getEstimatedRowCount()).intValue();elements = new SelectItem[length];SelectItem item = null;for (int i = 0; i < length; i++) {Row row = iterator.getRowAtRangeIndex(i);if (row != null) {item = new SelectItem();item.setValue(row.getAttribute("code").toString());item.setLabel(row.getAttribute("name").toString());}elements[i] = item;}return elements;}public void setSelectedValue(String selectedValue) {this.selectedValue = selectedValue;}public String getSelectedValue() {return selectedValue;}/*** Get the page bindings** @return*/public BindingContainer getBindings() {if (this.bindings == null) {FacesContext fc = FacesContext.getCurrentInstance();this.bindings =(BindingContainer)fc.getApplication().evaluateExpressionGet(fc,"#{bindings}",BindingContainer.class);}return this.bindings;}}
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.
tq..helpfull..keep posting bro
ReplyDelete