Thursday, October 28, 2010

ADF UI - Accessing properties file or resource bundle in java bean

If we directly refer to resource bundle in jsf page by using 'Select text resource..' option available, the c:set tag will be added to the jsff page and we'll use the bundle properties using it's 'var' as a reference.
<?xml version='1.0' encoding='windows-1252'?> <jsp:root xmlns:jsp="" version="2.1" xmlns:af="" xmlns:f="" xmlns:c=""> <c:set var="demouiBundle" value="#{adfBundle['com.demo.ui.DemoUiBundle']}"/> <af:panelGroupLayout id="pgl1"> <af:showDetailHeader text="#{demouiBundle['Header.Title']}" disclosed="true" id="sdh1"> <f:facet name="context"/> <f:facet name="menuBar"/> <f:facet name="toolbar"/> <f:facet name="legend"/> <f:facet name="info"/> </af:showDetailHeader> <af:showDetailHeader text="#{pageFlowScope.ExampleBean.headerTitle}" disclosed="true" id="showDetailHeader1"> <f:facet name="context"/> <f:facet name="menuBar"/> <f:facet name="toolbar"/> <f:facet name="legend"/> <f:facet name="info"/> </af:showDetailHeader> </af:panelGroupLayout> </jsp:root>

But, some times there might be needs to refer the properties bundle from java bean to access a property.

This can be done by using the below code.
package com.demo.beans; import java.util.ResourceBundle; import oracle.javatools.resourcebundle.BundleFactory; public class ExampleBean { public String getHeaderTitle() { ResourceBundle rs = BundleFactory.getBundle("com.demo.ui.DemoUiBundle"); return rs.getString("Header.Title"); } }

Content of the sample bundle file:
# sample properties Header.Title=Employee Details EMP_NAME=Employee Name DEPT_NAME=Department Name

Running the above jsff, we'll get the same text by using java bean as well direct reference in jsff (Screen shot below).


Related Posts with Thumbnails