2010年11月14日星期日

Reference the bindings with JSF managed bean in ADF

This post will demonstrate how to use the bindings variable in the backing bean of  the JSF page.

Also, the specific contents of the bindings variable and the page definition file’s details are explored.

Step 1. Add the bindings variable in the backing bean, if there is no backing bean for the page, just create one.

import oracle.adf.model.binding.DCBindingContainer;

import oracle.binding.OperationBinding;

public class BackingBean {

private DCBindingContainer bindings;

public void setBindings(DCBindingContainer bindings) {
    this.bindings = bindings;
}

public DCBindingContainer getBindings() {
    return bindings;
}

}

Step 2. Register the backing bean and also register the bindings field as the backing bean’s managed property.

 

<managed-bean>
    <managed-bean-name></managed-bean-name>
    <managed-bean-class >….</managed-bean-class>
    <managed-bean-scope>…</managed-bean-scope>
    <managed-property >     

<property-name>bindings</property-name>
<property-class>oracle.adf.model.binding.DCBindingContainer</property-class>
      <value>#{bindings}</value>
    </managed-property>
  </managed-bean>

Please pay attention to the managed property’s value and type.

Step 3. Now you can use the bindings in the backing bean.  For example:

OperationBinding operation = this.bindings.getOperationBinding("Commit");

operation.execute();

Please pay attention to the fact that the operation maybe is null, which is because there is no ‘Commit’ operation binding in the page. The operation binding should be configured at the page definition file.  We will explore the page definition in another session.

DCBindingContainer provides access to all contained bindings.

The following codes are just results of reasoning, not a tested one.

BindingContext may provides all the bindings configure in the data controls.

Through the Data control, the data provider , which maybe an application module can be accessed.

So the steps to access the application module in the JSF’s backing bean is as below:

FacesContext context = FacesContext.getCurrentInstance();
ELContext elContext = context.getELContext();
ValueExpression expression =
    context.getApplication().getExpressionFactory().createValueExpression(elContext,
                                                                          "#{data}",
                                                                          BindingContext.class);
BindingContext bc = (BindingContext)expression.getValue(elContext);
DCDataControl dcControl = bc.findDataControl("QuestionAMDataControl");
ApplicationModule am = (ApplicationModule)dcControl.getDataProvider();

Or we can access the BindingContext through DCBindingContext.

BindingContext bindingContext = bindings.getBindingContext();

Also the DCDataControl can also be found by the DCBindingContainer:

DCDataControl dbControl = bindings.findDataControl("QuestionAMDataControl");