Oracle ADF contextual events allows a component to publish an event and have multiple subscribers. A good use case would be if you have multiple regions within a JSF page and you want them to be able to communicate with one another. Granted, you could pass information around using page parameters, but if you’re working with many subscribers, this can become a very tedious process. This is just a quick Oracle ADF training tutorial to learn for free.
There are several types of contextual events.
- Attribute binding
- Range binding
- Action and method binding
- Event binding
Here are the basic steps to creating a contextual events for attribute changes. Watch the video to see all the details.
- Choose the component that is kicking off the event. Go to the Property Inspector and create a new contextual event.
- Decide which components will be receiving the events. The subscriber is typically a Java object that references the components via EL and managed beans. The component should have the binding attribute set to a managed bean property, as well as the value set to a bean property.
- Create the Java class that is the “subscriber”. If you are working with an attribute binding, you should have a method that takes one parameter – a DCBindingContainerValueChangeEvent object, which is the payload.
- Go to the page definition file for the region that contains the subscribers. Add a methodAction binding that points to the method that you created in the previous step.
- Go to the page definition file for the main page that contains the regions. Go to the Structure Window, then right-click on the root node. Select “Edit Event Map” from the context menu.
- Click the green plus icon to create a new map entry. Select the producer, event, and subscriber for the map entry. Set the name of the payload to the name of the method parameter your created. Set the value to the payload – you can find this in the EL Editor.
- Run your page!
Below is the code for the Java class containing the subscriber method:
import javax.el.*;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import oracle.adf.model.binding.DCBindingContainerValueChangeEvent;
import oracle.adf.view.rich.context.AdfFacesContext;
public class MyEventConsumer {
public MyEventConsumer() {
super();
}
public void handleEvent(DCBindingContainerValueChangeEvent payload){
FacesContext context = FacesContext.getCurrentInstance();
ELContext eLContext = context.getELContext();
Application application = context.getApplication();
ExpressionFactory expFactory = application.getExpressionFactory();
ValueExpression valExpression =
expFactory.createValueExpression(eLContext, “#{backingBeanScope.NameBean}”, Object.class);
NameBean nb = (NameBean)valExpression.getValue(eLContext);
String newValue = (String)payload.getNewValue();
nb.setLname(newValue);
AdfFacesContext adfCtx = AdfFacesContext.getCurrentInstance();
adfCtx.addPartialTarget(nb.getLnameField());
}
}