Thursday, 15 October 2015

Different ways to get UI Component values programatically in ADF

Objective: Now the main objective of this post is to show you in how many ways we can get the values of the UI components programmatically in bean.

We can see that many Software Developers asking questions on how to get the values of the ADF UI components in the bean. As of now I am aware of two ways to get the values of the UI component in bean.

The first way is to create the binding of the UI component to the bean and then accessing its value by calling this.getComponentBinding().getValue().toString().

The second way is by using af:clientAttribute inside the component that will fire the event to get the values, like af:button.

Requirement: I have a demo.jspx page. On that page I have two af:inputText, one af:outputText and one af:button components. The af:button names is: "Get Values” ,  and the af:inputText labels are "First Name" and "Last Name". Set the value of the af:outputText as value="#{viewcontrollerBundle.HELLO}".

Now when I click on the af:button, it should display the af:inputText values and the af:outputText value in the bean.

Solution: For solution of the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web application.

Step 2: Create a demo.jspx page in the ViewController project of our application.

Step 3: Drag and Drop one af:button, one af:outputText and two af:inputText from the component palette on the demo.jspx page.

Name the two af:inputText label as First Name and Last Name. Create the binding for the first af:inputText as binding="#{pageFlowScope.MyBean.firstNameBinding}".

Set the value of the second af:inputText as value="Deb"

Set the value of the af:outputText as value="#{viewcontrollerBundle.HELLO}".

Name the af:button as "Get Values". Create an actionListener for all the af:button. Thus, the actionListener for the created af:button is actionListener = "#{pageFlowScope.MyBean.getValues}". Now drag and drop af:clientAttribute inside the af:button. Set the value of the af:clientAttribute as “Deb”. The value should be exactly same as the value of the Last Name af:inputText component and give any name for the  af:clientAttribute. In this case I have give name="lastName". This will help us to get the value of the component in MyBean,java class

Similarly again drag and drop af:clientAttribute inside the af:button. Set the value of the af:clientAttribute as value="#{viewcontrollerBundle.HELLO}". The value should be exactly same as the value of the af:outputText component and give any name for the  af:clientAttribute. In this case I have given the name as name="greeting".

Thus, the complete demo.jspx page is shown below:

Step 4: Now go to the MyBean.java and write the codes for getValues method.Thus, the complete MyBean.java code is shown below:

package com.susanto;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputText;

public class MyBean {
    private RichInputText firstNameBinding;

    public MyBean() {
    }

    public void getValues(ActionEvent actionEvent) {
        String fname = this.getFirstNameBinding().getValue().toString();
        System.out.println("First Name is : " + fname);
        String lname = actionEvent.getComponent().getAttributes().get("lastName").toString();
        System.out.println("Last Name is : " + lname);
        String greet = actionEvent.getComponent().getAttributes().get("greeting").toString();
        System.out.println("The greeting message is : " + greet);
    }

    public void setFirstNameBinding(RichInputText firstNameBinding) {
        this.firstNameBinding = firstNameBinding;
    }

    public RichInputText getFirstNameBinding() {
        return firstNameBinding;
    }
}


Step 5: Save all and run the application. Thus, ran application is shown below:

Step 6: Now we will type some value say (Moumita) in the af:inputText component that is inside First Name af:inputText as shown below:

Now when I will click the "Get Values" then we get the values of the two af:inputText that is the values of the First Name and the Last Name as well as the value of the af:outputText printed in the MyBean.jav class as shown below:

Hence, the solution to our requirement.

If you really like the post and want to get updated of my upcoming posts, then join susantopaul.com on: Facebook and LinkedIn.

Please don’t forget to share and comment on the post.


Thanks & Regards,
Susanto Paul