Thursday, January 18, 2007

Accessing property file entry from ADF Faces bean

In some cases, developer need to access property file entries not only from expression language, but also from ADF Faces Backing or Managed bean code. There is nothing difficult, just notice, that message bundle must be specified in faces-config.xml

You can download sample application - AccessPropertyFile.zip, which shows how I'm accessing property file entries from Backing bean methods. There are two buttons on the application page and one text component. Entries in property file:
  1. firstAction=First action processed
  2. secondAction=Second action processed
Value for text component is retrieved from property file and depends on clicked button:


When developing described functionality, at first I have declared message bundle in faces-config.xml:
  1. Open faces-config.xml in JDeveloper
  2. Select Overview tab
  3. Choose Application option
  4. Declare property file in Message Bundle field (e.g. lt.andrejusb.resources.UIResources)
Second step was to create two af:commandButton components along with one af:outputFormatted, for both buttons action methods in Backing bean were generated. Code for action methods:


Entries in property file are accessed using function from JSFUtils class - getStringFromBundle(param). This become possible, after declaring message bundle in faces-config.xml. JSFUtils class source comes with standard JDeveloper sample application - SRDemo.

13 comments:

  1. Thank you for the article, this helped me a lot.

    from Venezuela,
    regards

    ReplyDelete
  2. thanks a lot but the link for the application is not working .

    thanks in advance .

    ReplyDelete
  3. Hi Andrejus,

    I need your help on the below scenario:

    I need a variable which is visible all through the application.
    I tried with sessionScope but I'm losing the value when the control goes from one ViewController project to another.

    I think I cant use the property file since the value in it needs to be changed.

    Could you please give your suggestions.

    Thanks,
    Dinesh

    ReplyDelete
  4. May be you can use Application Scope?

    Andrejus

    ReplyDelete
  5. Hi Andrejus,

    Thanks for the reply.

    I too tried with the applicationScope but still when the control goes from one View Controller to another, I lost the value.

    Thanks,
    Dinesh

    ReplyDelete
  6. Then you can use DB as repository store.

    Andrejus

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hi Andrejus,

    I'll try with the DB.
    Thank you.

    Thanks,
    Dinesh

    ReplyDelete
  9. I can't access the zipped application to see in which directory you store the property file.

    ReplyDelete
  10. This is quite old post... sorry I don't have this sample anymore...

    Andrejus

    ReplyDelete
  11. is it possible to read value from an external property file.

    as requirement is client want to keep property file at seperate place(due to several perprod/prod server).

    so using class loader i am trying to read external property file, i added file name in weblogic classpath in commEnv.cmd file also but not able to load property file from there.

    any help?

    Thanks in advance!!

    Reards
    Apoorv Jain

    ReplyDelete
  12. Yes, instead of using property file, you should use Java class for resource bundle. You can initialize it from any external file.

    Andrejus

    ReplyDelete
  13. Hi Andrejus,

    Is it possible to read a property file (which is not part of Adf app. package)from ADF

    Scenaio:I need a propery file which will be outside Adf app. If i want to make some changes i will make it in property file .so that each time if there is any changes i don't require to create EAR again for deployment.

    Thanks in advance
    Abhinav.

    ReplyDelete