Friday, September 27, 2013

Variable Iterator in ADF to store values between requests

Variable Iterator is a powerful features provided by ADF which is being often not taken into consideration during implementation of Use Cases.

By Using Variable Iterator , variables can be saved between the requests and they can be refereed just by bindings name inside you beans. So using variables to store values between request is the way where variables can be accessed inside managed beans both having loose coupling between each other. You do not need to define a property(variable) inside a bean and bind it to the UI component.

So lets get started and see how we can use the power of Variable Iterator in ADF. We will be starting from the scratch and going step by step for clear understanding.

The application can be downloaded Here



I created a simple ADF Web application as below. Created a jspx named variableIterExample.jspx.
Inside the page created three input text boxes named List Price , Quantity and Total Amount along with one button Calculate Total.


Now lets go to bindings of this page. In the executable section, click green + sign and select variableIterator from the window like below.


Now right click on the variable iterator and select "Insert Inside Variables" and select variable like below.


Now add name of variable and its type like below.



Now click on the created variable and provide its default value as 40 in the property inspector. We will be using this value as fixed List Price of the product.

So here comes the important concept which needs to be understood, rest all is easy.

The variables created inside variable iterator are actually value holders. They hold values which can be used between the requests and can be used in the page and managed beans. Just to test this, we have given listprice variable a default values as 40. This value should be available and be saved between requests.

In the same fashion 2 more attributes for quantity and total of type Integer are created. 

Now we know that variables inside variable iterator holds the values , but there needs to be something with which we can access these variables, as variables inside the variable iterator can not be accessed directly.

So we create a attributeValues in the binding section like below.




So the listPrice1 attribute binding basically points to listPrice variable inside the VariableIterator. This is basically a refrence which points to the actual variable listPrice variable in VariableIterator.

Similarly create all the three attributeValue bindings to the three variables as below.



Now we are set with bindings part. Lets go to the page and bind the input text boxes. We will bind the input text (UI Components ) to the refrence variables(attributeValues) in the bindings. 

So list price input text is binded to listPrice1.inputValue and simlarly other two.

Now click on button and create a bean and bind action listener of this button the action method.


Below is the code of the jspx page. Partial Submit of the button is made true and partial trigger of Total input text is made to reffer to this button. List price and total are made disable because we do not want user to change these two fields.



Below is the code implementation of the TotalBean and actionListener


Thats it, lets save our project and run the page. Below is the page.


Now see, the value of List Price is 40. We have bind the input text to listPrice1. But this is actually referring to variable listPrice in VariableIterator. So this shows, that values are saved and attributeValues is actually refer variable which points to the variable.


Now enter the quantity in Quantity text field and click Calculate Total.

And here is the result. 



Happy Learning !!




4 comments:

  1. Thanks a lot Rohan. I haven't used this beautiful functonality before. Going forward this functionality will help me a lot in my developement. Could you please suggest how to use variableUsage in execuatable.

    ReplyDelete
  2. Thanks Sunil, Glad you liked it.

    Thanks

    ReplyDelete
  3. This is great! Your tutorials helped me a lot! Thanks :)

    ReplyDelete