Follow by Email

Tuesday, 3 November 2015

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

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

name- Name of 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 way to do this

1. Get 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 f: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 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)


Sample ADF Application (Jdeveloper 12.2.1) -Download
Cheers :) Happy Learning

1 comment :

  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?

    ReplyDelete