SelectOneChoice Value

We will explain how to get the value of SelectOneChoice rather than index.
as figured in below picture

Assume we have a Model-Driven List for DepartmentId  attribute with the display value of DepartmentName and selectOneChoice bound to DepartmentId attribute in jspx page

Using Expression Language
the code of SelectOneChoice in page as below

<af:selectOneChoice value="#{bindings.DepartmentId.inputValue}"
                                                label="Select Department"
                                                shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1"
                                <f:selectItems value="#{bindings.DepartmentId.items}" id="si1"

I will add outputText for and get the values using Expression Language

Selected Index
<af:outputText value="Selected Index  : #{bindings.DepartmentId.selectedIndex}" id="ot1"
Selected Value
<af:outputText value="Selected Value : #{bindings.DepartmentId.attributeValue}" id="ot2"
Display Value
<af:outputText value="Display Value : #{bindings.DepartmentId.selectedValue.attributeValues[1]}" id="ot3"

the common mistake we do is to use the same EL value property of SelectOneChoice component, but using this we get the index of the selected item instead rather than the value. This is because when we drag and drop attribute as SelectOneChoice on to the page, SelectItems gets generated with indexes as values.

Using Backing Bean
Now I will explain how to get as previous example using backing bean as I will write code in ValueChangeListener

First step I will add to java bean the the below two methods
    public Object getElExpression(String el) {     
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =  facesContext.getApplication().getExpressionFactory();       
        ValueExpression valueExp = expressionFactory.createValueExpression(elContext,el,Object.class);
        return valueExp.getValue(elContext);

    public void setElExpression(String el, Object val) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ELContext elContext = facesContext.getELContext();
        ExpressionFactory expressionFactory =   facesContext.getApplication().getExpressionFactory();
        ValueExpression valueExp = expressionFactory.createValueExpression(elContext, el, Object.class);
        valueExp.setValue(elContext, val);

Add the below code in valueChangeListener of SelectOneChoice
public void valueChanged(ValueChangeEvent valueChangeEvent) {
this.setElExpression("#{bindings.DepartmentId.inputValue}", valueChangeEvent.getNewValue());
this.ot1.setValue("Selected Index : " + getElExpression("#{bindings.DepartmentId.selectedIndex}").toString());
this.ot2.setValue("Selected Value : " + getElExpression("#{bindings.DepartmentId.attributeValue}").toString());
this.ot3.setValue("Display Value : " +

You can download example from SelectOneChoice.rar

Mahmoud A. El-Sayed

Popular posts from this blog

ADF : Get Current Logged User Name

OAF : Get Current Row in Table

ADF : Working with ViewCriteria