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;
}

8 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. 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
  6. 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
  7. wonderful information, I had come to know about your blog from my friend nandu , hyderaba.
    i have read atleast 7 posts of yours by now, and let me tell you, your website gives the
    best and the most interesting information. This is just the kind of information that i had
    been looking for, i'm already your rss reader now and i would regularly watch out for the new posts.


    Oracle fusion HCM Online Training

    Oracle Fusion HCM Training

    ReplyDelete
  8. Thank you For Sharing Information
    Yaaron Studios is one of the rapidly growing editing studios in Hyderabad. We are the best Video Editing services in Hyderabad. We provides best graphic works like logo reveals, corporate presentation Etc. And also we gives the best Outdoor/Indoor shoots and Ad Making services.
    Best video editing services in Hyderabad,ameerpet
    Best Graphic Designing services in Hyderabad,ameerpet­
    Best Ad Making services in Hyderabad,ameerpet­

    ReplyDelete

Protected by Copyscape Unique Content Check