Tuesday, March 27, 2012

Access Resource Bundle in Bean

   
    To access resource bundle in bean

    public String getTextProp(){
        String bundle_name= "com.gppsuite.view.ViewControllerBundle";
        ResourceBundle resourceBundle =   BundleFactory.getBundle(bundle_name);
        String txt =null;
        if(resourceBundle != null)
        {
        txt = resourceBundle.getString("string_id");
        }
        return txt;
    }


Thanks Lakshman for your comments.
Including your comments in the post for the benefit of others.

2)

We can also use
java.util.ResourceBundle rb = ResourceBundle.getBundle("com/oracle/,,,,/Bundle1");
String txt = rb.getString("prop1");

3)
public String readPropertiesFile(String property){
String configureURL = null;
try{
InputStream is = ConfigureBean.class.getResourceAsStream("Engenio.properties");
Properties prop = new Properties();
prop.load(is);
configureURL = prop.getProperty(property);
is.close();
/* code to use values read from the file*/
}catch(Exception e){
e.printStackTrace();

}
return configureURL;
}

7 comments:

  1. We can also use
    java.util.ResourceBundle rb = ResourceBundle.getBundle("com/oracle/,,,,/Bundle1");
    String txt = rb.getString("prop1");

    ReplyDelete
  2. Use can also try this

    public String readPropertiesFile(String property){
    String configureURL = null;
    try{
    InputStream is = ConfigureBean.class.getResourceAsStream("Engenio.properties");
    Properties prop = new Properties();
    prop.load(is);
    configureURL = prop.getProperty(property);
    is.close();
    /* code to use values read from the file*/
    }catch(Exception e){
    e.printStackTrace();

    }
    return configureURL;
    }

    ReplyDelete
  3. The second method is actually the same as your first one. Except that first one is better since the second one has a greater chance of failing and throwing null pointers exceptions.

    ReplyDelete
  4. if the properties file "name.preperties" is directly under src folder, the method ResourceBundle resourceBundle = BundleFactory.getBundle("name") is not working, why ?

    ReplyDelete
  5. Thank to share.It feels nice to read you blog throughly...

    Regards:
    Qadir Shaikh.
    Visit at http://www.oratc.com

    ReplyDelete
  6. Thanks for your post.

    Quick question:
    Is there a best practice when handling null properties (i.e., if you are using the .properties file inputs as text in a UI application, and setting the text from the properties on the server-side)?

    Just curious.

    ReplyDelete
  7. It was so nice article and useful to Informatica learners. we also provide oracle ADF course online training our Cubtraining is leader in providing Software Training

    ReplyDelete

Protected by Copyscape Unique Content Check