我使用Apache配置进行了以下配置设置:
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
Configuration config = new PropertiesConfiguration("config.properties");
我想知道在属性文件中是否有某种方法可以使用占位符?例如,我想要这个:
some.message = You got a message: {0}
并且能够传递{0}
占位符的值。通常情况下,您通常可以执行config.getString("some.message", String[] of values)
之类的操作,但不要看到类似的内容。
答案 0 :(得分:1)
我相信您可以通过属性文件中的占位符获取属性值,然后使用MessageFormat将消息格式化为您想要的内容。假设您在属性文件中有以下属性:
some.message = "Hey {0}, you got message: {1}!"
这样你就可以获得并格式化这个属性,如:
message will be "Hey {0}, you got message: {1}!"
String message = config.getString("some.message");
MessageFormat mf = new MessageFormat("");
//it will print: "Hey Thomas, you got message: 1"
System.out.println(mf.format(message, "Thomas", 1));
此外,如果您可以使用系统变量或环境变量,如果它没有动态更改
答案 1 :(得分:1)
据我所知,Configuration类不提供任何格式化程序。所以,为了你的任务
您可以按照Bilguun的建议使用MessageFormat。请 请注意,该格式方法为静态。
您可以使用Trace:
#0 /home/tantransh/public_html/test/lib/Zend/Controller/Response/Abstract.php(115): Zend_Controller_Response_Abstract->canSendHeaders(true)
#1 /home/tantransh/public_html/test/app/code/core/Mage/Core/Model/App.php(1246): Zend_Controller_Response_Abstract->setHeader('Content-Type', 'text/html; char...')
#2 /home/tantransh/public_html/test/app/code/core/Mage/Core/Controller/Varien/Front.php(80): Mage_Core_Model_App->getResponse()
#3 /home/tantransh/public_html/test/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(202): Mage_Core_Controller_Varien_Front->getResponse()
#4 /home/tantransh/public_html/test/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#5 /home/tantransh/public_html/test/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#6 /home/tantransh/public_html/test/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#7 /home/tantransh/public_html/test/index.php(87): Mage::run('', 'store')
#8 {main}
功能。
示例如下:
<强> config.properties 强>
String.format
示例课程
enter some.message.printf=%1$s\, you've got a message from %2$s \n
some.message.point.numbers =Hey {0}\, you got message: {1}!