简体   繁体   中英

selectOneChoice in ADF

I am new to ADF and facing some issues: I have an af:selectOneChoice component in my page, which contains a list of data.

I want to get the selected value when an user selects one option from the list of selectOnceChoice component.

I have used request bean with a parameter and bind it in the value option in property inspector of the component, but it failed to give the selected value. How can i get the selected value from the selectOneChoice component?

You can use ValueChangeListener of selectOnceChoice component. In valueChangeListener you can bind it to a bean class method and call API to get the new or old value for selectOnceChoice component:

public void selectOnceChoiceValue(ValueChangeEvent valueChangeEvent){
    if(valueChangeEvent != null) {
        Object newVal = valueChangeEvent.getNewValue();
        Object oldVal = valueChangeEvent.getOldValue();
    }
}
<af:selectOneChoice id="soc1" simple="true" autoSubmit="true"
   valueChangeListener="#{ExtnEmailNotificationPFBean.recipientTypeValue}"
   label="#{''}"
   binding="#{ExtnEmailNotificationPFBean.recipientTypeValue}"
   partialTriggers="cl2"
   value="#{pageFlowScope.ZcxEaNotificationRecipientType}">
  <f:selectItems id="si4"
          value="#{pageFlowScope.ZcxEaRecipientsTypeValueList}"/>                  
</af:selectOneChoice>

Depending on what you need to do with the value one option is the following methods in your backing bean

public void valueChanged(ValueChangeEvent valueChangeEvent) {
    this.setValueToEL("#{bindings.Deptno.inputValue}", valueChangeEvent.getNewValue()); //Updates the model
    System.out.println("\n******** Selected Value: "+resolveExpression("#{bindings.Deptno.attributeValue}"));
    System.out.println("\n******** Display Value: "+resolveExpression("#{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}"));
}

public Object resolveExpression(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 setValueToEL(String el, Object val) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ELContext elContext = facesContext.getELContext();
    ExpressionFactory expressionFactory =   facesContext.getApplication().getExpressionFactory();
    ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
    exp.setValue(elContext, val);
}

and you JSP component should look like

<af:selectOneChoice value="#{bindings.Deptno.inputValue}" label="Select Department"
                            required="true" shortDesc="#{bindings.Deptno.hints.tooltip}"
                            id="soc1" autoSubmit="true">
          <f:selectItems value="#{bindings.Deptno.items}" id="si1"/>
</af:selectOneChoice>

From http://blogs.oracle.com/adf/entry/getting_selected_value_from_selectonechoice

This will get you the displayed text in real time, if you're looking more for a connection to the view object backing the component you'll want to look at something like.

public void buttonPressed(ActionEvent actionEvent) {

    // Get the binding
    BindingContainer bindings =
    BindingContext.getCurrent().getCurrentBindingsEntry();
    // Get the sepecific list binding
    JUCtrlListBinding listBinding =
    (JUCtrlListBinding)bindings.get("DepartmentId");
    // Get the value which is currently selected
    Object selectedValue = listBinding.getSelectedValue();
    System.out.println(selectedValue);
}

From http://blogs.oracle.com/shay/entry/getting_the_value_from_a_selec

If I got it right, your problem is that value, you have selected with af:selectOneChoice will be available at bean, only after submit. If thats the case, then you should either let user submit the form, or set autoSubmit property of component to true .

This should do :

<af:selectOneChoice id="sampleId" 
value="#{bindings.sampleAttribute.inputValue}"
label="#{bindings.sampleAttribute.label}"
valueChangeListener=#"{Bean.valueChangeListener}"
immediate="true" autoSubmit="true">
    <f:selectItems id="sampleAttributeValues"
    value="#{bindings.sampleAttribute.items}"/>
</af:selectOneChoice>

and the bean would have :

public void valueChangeListener(ValueChangeEvent valueChangeEvent)
{
    FacesContext.getCurrentInstance().renderResponse();  
}

If you use the autoSubmit property to "true" make sure to set the immediate to "true" as well. Otherwise, validation would be fired at the page level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM