Wednesday, November 3, 2010

How To Access Page Element Value from Backing Bean

I have received question from ADF newbie developer, who is starting to work with the framework. Question was simple, however I have decided to post it on the blog - simple topic for a change. It happens we work too much on complex things and forget about simple ones. Today I will describe how you can access Page Element (Input Text, etc.) value from Backing Bean.

First thing, you should register Java class as a bean. If you are working with page/fragment from bounded task flow, register it under that task flow:


There is Binding property in Advanced section, for every Page Element, it will reference component binding accessible from the bean class. I will use DepartmentName attribute in this example. Also I will declare my Page Element to have auto submit functionality - this will invoke Value Change Listener:


For the demonstration purpose, I will declare one more DepartmentName attribute value in page definition - DepartmentNameTest:


There are four different approaches implemented in the bean class. First is accessing element binding and getting its value. Another approach works with current row from iterator. Third approach is accessing attribute value from page definition. I prefer this approach, it makes implementation more loosely coupled and independent from the model refactoring. There is fourth approach as well - to get new value from value change listener directly:


We can press Test button, DepartmentName value will be accessed in the bean class:


Value is accessed:


DepartmentName value is changed:


Accessed from value change listener:


Download sample application - InputTextValueAccess.zip

12 comments:

  1. thank you andre, now I become more familiar....:)

    ReplyDelete
  2. hello,
    I've tried your solution via component binding. When I use setValue, the value of component instance is changed, but did not enforce the one in the page to refresh.

    How can I refresh the component on the page?

    ReplyDelete
  3. Hi,

    It works well for me, just by adding mentioned code into testAction() method.

    Regards,
    Andrejus

    ReplyDelete
  4. You can use this to refresh a component:
    AdfFacesContext.getCurrentInstance().addPartialTarget(bindingcomponent);

    ReplyDelete
  5. Hi,

    I have a question regarding basic ADF.


    how to add two rows to a table on one commit button click?
    my case here is i have a customer address table and Primary address VO and Correspondence address vo on ui.
    and m differentiating them by a flag Addresstype in table.On click of commit button primary address should go as one record and correspondence address as other record.

    Hope u got my problem...

    Thanks

    ReplyDelete
  6. Hi,

    Yes, you would need to do insert programmatically. Let me know if you need a sample.

    Andrejus

    ReplyDelete
  7. Thanks for this!

    How does it change if I need to access the value from Rich Text Editor along with the html formatting.

    Please help :)

    ReplyDelete
  8. How it should change actually?

    Andrejus

    ReplyDelete
  9. It is not highlighted that the InputText should have autoSubmit="true" or you will get nulls with all 4 methods listed.

    ReplyDelete
  10. It is highlighted to use autoSubmit - check second screenshot and description text above it.

    Andrejus

    ReplyDelete
  11. Hi Can you please upload your old projects again ? when ever I want to download one it gives me 404 error. Thanks

    Ferdous

    ReplyDelete
  12. All old samples (prior to 2014) are available from Google Archive: https://code.google.com/archive/p/jdevsamples/downloads

    Regards,
    Andrejus

    ReplyDelete