ADF Basics: Using f:attribute to pass parameter in ActionEvent

Sharing is Caring

f:attribute tag (JSF Tag supported in ADF Faces) is used to pass some additional attribute value to the associated component
Sometimes using f:attribute simplify a complex piece of code, this tag has a very simple structure. It has two properties

name- Name of the tag attribute
value- Value or an EL reference of value

Here in this post, we will see how to use this tag with ADF Faces, I am using Departments table of HR Schema and requirement is to delete departments with attribute DepartmentId  greater than 100


So I have added a link in af:table to delete selected department
Now on this delete link action, I have to check that DepartmentId for selected row should be greater than 100 and there are multiple ways to do this

1. Get the current row of iterator and get DepartmentId from that row
2. Get selected row using getSelectedRowKeys

But here I am using f:attribute to get selected DepartmentId, See how to do this 🙂

Add the attribute tag under af:link like this, here I have assigned #{row.DepartmentId} as value reference of f:attribute tag, this will store selected departmentId in attribute

<af:column id="c5">
                        <af:link text="Delete" id="l1"
                                 actionListener="#{viewScope.FAttributeDemoBean.deleteDepartmentAction}">
                            <f:attribute name="DepartmentId" value="#{row.DepartmentId}"/>
                        </af:link>
                    </af:column>

Now see how to get f:attribute value in the managed bean and perform delete operation on that basis

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

/**Get BindingContainer of current view port**/
public BindingContainer getBindingsCont() {
return BindingContext.getCurrent().getCurrentBindingsEntry();
}

/**
 * Generic Method to execute operation Binding
 * */
public OperationBinding executeOperation(String operation) {
OperationBinding createParam = getBindingsCont().getOperationBinding(operation);
return createParam;

}

/**Method to delete Department row on condition basis
 * @param actionEvent
 */
public void deleteDepartmentAction(ActionEvent actionEvent) {
//Get attribute value using it's name
Object obj = actionEvent.getComponent().getAttributes().get("DepartmentId");
if (obj != null && ((Integer) obj) > 100) {
// If department id is greater than 100 then call delete operation on it
executeOperation("Delete").execute();
executeOperation("Execute").execute();
} else {
FacesMessage errMsg = new FacesMessage("Department Id for selected Department is not greater than 100");
errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, errMsg);
}
}

All done :D, Run application and check (tried deleting Department with DepartmentId 100)

f:attribute

Sample ADF Application (JDeveloper 12.2.1) –Download
Cheers 🙂 Happy Learning

Related Posts

An Oracle ACE, Blogger, Reviewer, Technical Lead working on Oracle ADF

0 thoughts on “ADF Basics: Using f:attribute to pass parameter in ActionEvent”

  1. Thank you. very good.
    Is there a way to pass attribute when no action listener is used, but want to use bean method and pass row param to this method?

Leave a Reply

Your email address will not be published. Required fields are marked *