How to declaratively instruct ADF Faces RC to partially refresh other components upon an event

1

 

ADF Faces RC has built in support for AJAX and Partial Page Refresh. Through the autoSubmit and partialSubmit attributes, we can indicate that a value change or an action (button or link click) should trigger an asynchronous request (PPR request). Through the partialTriggers attribute, we can configure components to be refreshed in any PPR request that is initiated by the a component whose Id is listed in the partialTriggers attribute. So you can say that through the partialTriggers attribute, we register a component as PPR listener on all components listed in the attribute value.

Sometimes, developers would like to be able to do it the other way round. To specify on a component X which other components should be refreshed when X is initiating a PPR request. In this article, we will discuss a method for doing exactly that.We will use a very simple example of an outputText that displays the value of the counter property of a managed bean. The page contains a commandLink. When this link is clicked, the counter is incremented and we would like the outputText to be (partially) refreshed.

The usual approach for this: set partialSubmit to true for the link and include the commandLink component’s ID value in the partialTriggers attribute of the outputText:

        <af:panelLabelAndMessage id="counterLabel" label="Counter"
                                 for="counter">
          <af:outputText id="counter" value="#{CounterBean.counter}"
                         <strong>partialTriggers=&quot;increment&quot;</strong> /&gt;
        &lt;/af:panelLabelAndMessage&gt;
        &lt;af:commandLink <strong>id=&quot;increment&quot;</strong> text=&quot;Increase and Refresh Counter&quot;
                        actionListener=&quot;#{CounterBean.increaseCounter}&quot;
                        <strong>partialSubmit=&quot;true&quot;&gt;</strong>
        &lt;/af:commandLink&gt;

In this case, we will turn that around. The instruction to refresh the outputText when the link is activated is added to the commandLink.

        &lt;af:panelLabelAndMessage id=&quot;counterLabel&quot; label=&quot;Counter&quot;
                                 for=&quot;counter&quot;&gt;
          &lt;af:outputText id=&quot;counter&quot; value=&quot;#{CounterBean.counter}&quot;/&gt;
        &lt;/af:panelLabelAndMessage&gt;
        &lt;af:commandLink <strong>id=&quot;increment&quot;</strong> text=&quot;Increase and Refresh Counter&quot;
                        actionListener=&quot;#{CounterBean.increaseCounter}&quot;
                        partialSubmit=&quot;true&quot;&gt;
          &lt;af:setPropertyListener from=&quot;counter&quot;
                                  to=&quot;#{PartialTargetManager.partialTarget}&quot;
                                  type=&quot;action&quot;/&gt;
        &lt;/af:commandLink&gt;

The mechanism used in this approach is a generic PartialTargetManager with a setPartialTarget method that accepts a string that should hold the id for the component that should be refreshed. The setPropertyListener will fire when the link is activated (type=’action’) and will pass the string value "counter" (the id of the outputText component) to this setter method.

The PartialTargetManager bean is implemented as follows:

public class PartialTargetManager {
    public void setPartialTarget(String componentId) {
        UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent(componentId);
        if (component!=null) {
          AdfFacesContext context = AdfFacesContext.getCurrentInstance();
          context.addPartialTarget(component);
        }
    }
}

The setPartialTarget method tries to find the component specified in the ViewRoot. When found, the component is passed to the list of partial targets in the AdfFacesContext.

The bean is configured in faces-config.xml:

  &lt;managed-bean&gt;
    &lt;managed-bean-name&gt;PartialTargetManager&lt;/managed-bean-name&gt;
    &lt;managed-bean-class&gt;view.PartialTargetManager&lt;/managed-bean-class&gt;
    &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt;
  &lt;/managed-bean&gt;
  &lt;managed-bean&gt;
    &lt;managed-bean-name&gt;Counter&lt;/managed-bean-name&gt;
    &lt;managed-bean-class&gt;view.Counter&lt;/managed-bean-class&gt;
    &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;
  &lt;/managed-bean&gt;


The Counter bean is trivial:

public class Counter {

    private Integer counter =0;

    public Integer getCounter() {
        return counter;
    }

    public void increaseCounter(ActionEvent event) {
        counter++;
    }
}

When we run the page, the outputText and its label are displayed as well as the action link. Clicking the action link will lead to an increased value in the counter value – through PPR that was declaratively instructed from the perspective of the link to refresh the outputText.

Resources

Download JDeveloper 11g sources componentToComponentPPR.zip.

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Can this be done cross two pages? The use case is when the command link is on a search page while the counter or a ui component is on the edit page needs to be refreshed after navigated from the search to edit page.