1

I have a property files message_en.properties message_de.properties in my project and I have entry InfoBox_description=welcome to the this application and corresponding value in German. Now user should be allowed to change the description dynamically and same should reflect in other property file also with right conversion.how should I do this? I am using JSF2 and prime faces

Thanks in advance

shreekanth
  • 459
  • 2
  • 12
  • 27
  • If something should be changed dynamically, you shouldn't use properties. Rethink your design because that what you're planning to do is really bad idea. – Danubian Sailor May 30 '13 at 11:44
  • Possible duplicate of [Where to place configuration properties files in a JSP/Servlet web application?](http://stackoverflow.com/questions/2161054/where-to-place-configuration-properties-files-in-a-jsp-servlet-web-application) Although in your specific case, I think storing in DB and using a custom resource bundle control is a much better solution and this way you've also better control over caching. – BalusC May 30 '13 at 13:53
  • @LukŁukasz Lech: I agree with you it really does not make sense to but in my case user does not frequently change the description, but very few times and he wants do it dynamically and want conversion also using properties file. – shreekanth May 31 '13 at 02:07

1 Answers1

0

Assuming you have your properties file inside your war, in a package in the source folder, this is how you load it in the managed bean:

Properties properties = new Properties();
try {
   ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
   properties.load(classLoader.getResourceAsStream("/yourfile.properties"));
} catch (IOException e) {
    // handle exception
}

// then initialize attributes with the contents of properties file. Example:
property1 = properties.getProperty("property1");
property2 = properties.getProperty("property2");

Then, when user updates values (submit forms) and you want to update the properties file, do the following:

properties.setProperty("property1", property1);
properties.setProperty("property2", property2);

try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("/yourfile.properties");
    properties.store(new FileOutputStream(new File(url.toURI())), null);
 } catch (Exception e) {
     // handle exception
 }

If the properties file is not in the war (it's in the file system of the server) it is also possible but a little more complex..

EDIT: as BalusC explained, having the properties file that changes inside your war is not convenient because if you redeploy your war, then changes are lost. You could put the properties file in the file system of the server if you have access to it; or don't use properties files (use database instead)

damian
  • 4,024
  • 5
  • 35
  • 53
  • The webapp's `/resources` folder is not covered by the classpath. Plus, writing this way does not work at all when WAR is not expanded on disk file system, but instead in memory. Even if it works, all changes would get lost when WAR is redeployed or even when server is restarted, for the simple reason that those changes are not contained in the original WAR. – BalusC May 30 '13 at 13:53
  • thanks @BalusC for your comments. I implemented this some time ago, I couldn't remember were I placed the properties file. I guess it was in a package in source folder then. In my implementation I used some server configuration to access a properties file in the server file system, so configuration would not be lost after a redeploy, but the question did not specify this – damian May 30 '13 at 18:55
  • @BalusC: If user changes dynamically in English,now how do i reflect the change in German as well? vice versa is also possible. Are there any API for this conversion – shreekanth May 31 '13 at 02:11