Follow by Email

Thursday, 21 April 2016

ADF Basics: Get display and base value of POJO based SelectOneChoice

Previously I have posted about populating selectOneChoice programmatically using POJO
and this post is about getting selected value of POJO base selectOneChoice (both display value and base value)

We can get base value of selectOneChoice simply using value property but to get display value we have to iterate over list of items and find display value on basis of Base value
So here I have created a POJO based selectOneChoice , for that declared a List in managed bean to populate list items and a String variable to hold selected value of choice list

    //String variable to hold selected value of selectOneChoice
    String selectedVal;

    public void setSelectedVal(String selectedVal) {
        this.selectedVal = selectedVal;
    }

    public String getSelectedVal() {
        return selectedVal;
    }

    //List variable to populate selectOneCoice
    List<SelectItem> customList;

    public void setCustomList(List<SelectItem> customList) {
        this.customList = customList;
    }

    public List<SelectItem> getCustomList() {
        if (customList == null) {
            customList = new ArrayList<SelectItem>();
            customList.add(new SelectItem("i1", "Item 1"));
            customList.add(new SelectItem("i2", "Item 2"));
            customList.add(new SelectItem("i3", "Item 3"));
            customList.add(new SelectItem("i4", "Item 4"));
            customList.add(new SelectItem("i5", "Item 5"));
        }
        return customList;
    }

and how this List and String variable is mapped on page-


<af:selectOneChoice label="Select Value" id="soc1"
                                            value="#{viewScope.GetValuePojoListBean.selectedVal}"
                                            contentStyle="font-weight:bold;color:darkgreen;">
                            <f:selectItems value="#{viewScope.GetValuePojoListBean.customList}" id="si1"/>
                        </af:selectOneChoice>

Then dropped a button on page and created an actionListener in managed bean to get both values


    /**Method to get Selected Value in selectOneChoice
     * @param actionEvent
     */
    public void getDisplayAndBaseValueFromList(ActionEvent actionEvent) {
        //Iterate over List to get Display Value
        for (SelectItem si : customList) {
            if (si.getValue().toString().equalsIgnoreCase(selectedVal)) {
                System.out.println("SELECTED VALUE IS- " + selectedVal + " DISPLAY VALUE IS- " + si.getLabel());
            }
        }
    }

Now run and check application, Page looks like this-


Select a value and click on button-


Output on console-

Sample ADF Application (Jdev 12.1.3)- Download
Cheers :) Happy Learning

No comments :

Post a Comment