18 August, 2011

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"
                                                required="#{bindings.DepartmentId.hints.mandatory}"
                                                shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1"
                                                binding="#{backingBeanScope.backing_SelectOneChoice.soc1}"
                                                autoSubmit="true">
                                <f:selectItems value="#{bindings.DepartmentId.items}" id="si1"
                                               binding="#{backingBeanScope.backing_SelectOneChoice.si1}"/>
                            </af:selectOneChoice>


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

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

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 : " +
getElExpression("#{bindings.DepartmentId.selectedValue.e.attributeValues[1]}").toString());
}

You can download example from SelectOneChoice.rar

Thanks
Mahmoud A. El-Sayed

No comments:

Post a Comment

ADF : Scope Variables

Oracle ADF uses many variables and each variable has a scope. There are five scopes in ADF (Application, Request, Session, View and PageFl...