Reputation: 93
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?
Upvotes: 0
Views: 16001
Reputation: 5803
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.
Upvotes: 0
Reputation: 5803
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();
}
Upvotes: 0
Reputation: 1804
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
.
Upvotes: 0
Reputation: 542
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
Upvotes: 1
Reputation: 31
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>
Upvotes: 3