[英]Java: How do I add properties from a database to a ResourceBundle on runtime?
Background 背景
I am developing a Java Web Application using Maven. 我正在使用Maven开发Java Web应用程序。 I have created ResourceBundles in this path and format: src/main/resources/i18n/LanguageBundle_xx_XX.properties .
我已在此路径和格式中创建了ResourceBundles: src / main / resources / i18n / LanguageBundle_xx_XX.properties 。
Everything works perfectly and I have everything working on the front end (using JSTL). 一切都很完美,我在前端工作(使用JSTL)。
Problem 问题
The user can create KEY/VALUE pairs, from the front end, that are then stored in a MySQL database in the format: KEY | 用户可以从前端创建KEY / VALUE对,然后以以下格式存储在MySQL数据库中: KEY | VALUE
值
I have a initializing servlet that does all the setting up before taking the user to the first jsp page. 我有一个初始化servlet,在将用户带到第一个jsp页面之前完成所有设置。
In that initializing servlet I am getting all the KEY/VALUE pairs from the database, but now I want to add them to the LanguageBundle so that translations are ready when the first jsp page is presented. 在初始化servlet中,我从数据库中获取所有KEY / VALUE对,但现在我想将它们添加到LanguageBundle中,以便在显示第一个jsp页面时准备好翻译。
I've tried so far: 我到目前为止尝试过:
Now, that does add the property, however if I try to access that specific property outside that scope (eg, other servlets, jsp), it fails as it doesn't exist in the specific .properties file. 现在,这确实添加了属性,但是如果我尝试访问该范围之外的特定属性(例如,其他servlet,jsp),它将失败,因为它在特定的.properties文件中不存在。
How do I solve this problem? 我该如何解决这个问题?
UPDATE: 更新:
I have come up with a solution that works. 我想出了一个有效的解决方案。
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/i18n/LanguageBundle_en_US.properties");
prop.load(in);
prop.setProperty("KEY", "VALUE");
in.close();
prop.store(new FileOutputStream(getClass().getClassLoader().getResource("/i18n/LanguageBundle_en_US.properties").getFile()), null);
Can someone tell me if this is bad practice or is it fine to use? 有人可以告诉我这是不好的做法还是可以使用?
Thank you. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.