动态编辑属性文件 [英] dynamically editing property file

查看:73
本文介绍了动态编辑属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个属性文件message_en.properties message_de.properties,并且该应用程序有InfoBox_description = welcome条目,并具有相应的德语值.现在应该允许用户动态更改描述,并且也应该通过正确转换将其反映在其他属性文件中.我该怎么做?我正在使用JSF2和素面

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

预先感谢

推荐答案

假设您在战役中拥有属性文件,并位于源文件夹中的包中,这就是将其加载到托管bean中的方式:

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..

编辑:如BalusC所述,在战争中更改属性文件并不方便,因为如果重新部署战争,更改就会丢失.如果可以访问,可以将属性文件放在服务器的文件系统中;否则,可以将属性文件放置在服务器的文件系统中.或不使用属性文件(改为使用数据库)

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)

这篇关于动态编辑属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆