简体   繁体   English

Java:如何在运行时将数据库中的属性添加到R​​esourceBundle?

[英]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: 我到目前为止尝试过:

  1. Returning all the key/value pairs from the DB 从DB返回所有键/值对
  2. Iterating the values returned 迭代返回的值
  3. If KEY doesn't exist in LanguageBundle_xx_XX 如果LanguageBundle_xx_XX中不存在KEY
  4. Add the KEY/VALUE with property.put("KEY","VALUE") 使用property.put(“KEY”,“VALUE”)添加KEY / VALUE

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.

相关问题 Java:如何将图像从文件添加到ResourceBundle? - Java: How can I add images from a file into a ResourceBundle? 如何避免Java ResourceBundle字符串中的重复? - How do I avoid repetition in Java ResourceBundle strings? 如何从ResourceBundle切换到Properties(类)? - How to switch from ResourceBundle to Properties (class)? 如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们? - How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray? 如何从Java 9中的另一个模块获取ResourceBundle? - How to get ResourceBundle from another Module in Java 9? 如何使用Java将属性添加到属性文件? - How do I add properties to a properties file using Java? Java ResourceBundle.getBundle如何加载属性文件? - How does Java ResourceBundle.getBundle load properties file? 如何在JAVA中通过ResourceBundle将匈牙利字符用作属性文件中的键? - How to use Hungarian characters as a key in properties file with ResourceBundle in JAVA? 从 ResourceBundle 生成属性 object? - Generate a Properties object from a ResourceBundle? 如何使用ResourceBundle来避免Java应用程序中的硬编码配置路径? - How do I use ResourceBundle to avoid hardcoded config paths in Java apps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM