Oracle ADF – Using the variables iterator from pages data bindings

February 4th, 2011 | Posted by in Oracle ADF | 1 Comment

This post shows how to use the variables iterator which is available in the ADF page definition. These variables could be used when the dynamic information from a particular page are not persisted but only saved temporally for custom processing in a managed bean.

For this purpose I have created a basic application which has a form where you could calculate the sum of two numbers.

The main page:

If you are selecting the “Sum” button the page should display the sum of the two numbers:

The form has two input text fields for the two operands, one button for the operation and finally an output text for the result.
The question is how to bind these to the page definition in order to work with them in the managed bean in order to have the lowest coupling between the view and the managed bean (view model).
One solution is to create variables and attributes in the page definition descriptor and bind them together. These attributes are accessible in the ADF page through EL in the form:
#{bindings.<var_name>}.
The page definition source is listed below:

...
  <executables>
    <variableIterator id="variables">
      <variable Name="number1" Type="oracle.jbo.domain.Number"/>
      <variable Name="number2" Type="oracle.jbo.domain.Number"/>
      <variable Name="result" Type="oracle.jbo.domain.Number"/>
    </variableIterator>
  </executables>
  <bindings>
    <attributeValues IterBinding="variables" id="Number1">
      <AttrNames>
        <Item Value="number1"/>
      </AttrNames>
    </attributeValues>
    <attributeValues IterBinding="variables" id="Number2">
      <AttrNames>
        <Item Value="number2"/>
      </AttrNames>
    </attributeValues>
    <attributeValues IterBinding="variables" id="Result">
      <AttrNames>
        <Item Value="result"/>
      </AttrNames>
    </attributeValues>
  </bindings>
...

In order to add a variable to the variables iterator in the JDeveloper IDE you should go to the ADF page definition overview and right-click on the “variables” item available in the “Executables” column and from the popup menu select the “Insert inside variables -> variable” sub-menu.
Then, you can add the attributes corresponding to these variables in the “Bindings” column by using the “+” button.
Below you have the page definition overview:

Now, if you open the Structure window you will see the newly created variables:

The page contains the following source code:

...
  <af:form id="f1">
        <af:panelStretchLayout id="psl1">
          <f:facet name="center">
            <af:panelFormLayout id="pfl1">
              <f:facet name="footer">
                <af:group id="g1">
                  <af:commandButton text="Sum" id="cb1"
                                    action="#{Calculator.sum}"/>
                  <af:panelBorderLayout id="pbl1">
                    <f:facet name="start">
                      <af:outputLabel value="Result:" id="ol1"/>
                    </f:facet>
                    <f:facet name="bottom"/>
                    <f:facet name="end"/>
                    <f:facet name="top"/>
                    <af:outputText value="#{bindings.Result.inputValue}" id="ot1"/>
                  </af:panelBorderLayout>
                </af:group>
              </f:facet>
              <af:inputText label="Number 1" id="it1" rows="1" columns="20"
                            value="#{bindings.Number1.inputValue}"/>
              <af:inputText label="Number 2" id="it2" columns="20"
                            value="#{bindings.Number2.inputValue}"/>
            </af:panelFormLayout>
          </f:facet>
        </af:panelStretchLayout>
      </af:form>
...

The source code corresponding to the #{Calculator.sum} EL-expression from the managed bean is:

...
    public String sum() {

      FacesContext context = FacesContext.getCurrentInstance();
      Application app = context.getApplication();
      DCBindingContainer binding = (DCBindingContainer) app.evaluateExpressionGet(context, "#{bindings}",DCBindingContainer.class);
      AttributeBinding attrNumber1 = (AttributeBinding)binding.getControlBinding("Number1");
      AttributeBinding attrNumber2 = (AttributeBinding)binding.getControlBinding("Number2");
      AttributeBinding attrResult = (AttributeBinding)binding.getControlBinding("Result");

      Number number1 = (Number)attrNumber1.getInputValue();
      Number number2 = (Number)attrNumber2.getInputValue();
      attrResult.setInputValue(number1.add(number2));
        return null;
    }
...

Tags: , , ,

One Response to “Oracle ADF – Using the variables iterator from pages data bindings”

  1. Arunkumar says:

    Good Example for ADF Beginners.

Leave a Reply