java中属性中的动态占位符替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2205627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic Placeholder substitution in properties in java
提问by manish
I wanted to substitute the placeholder dynamically in properties in a java application. Like
我想在 Java 应用程序的属性中动态替换占位符。喜欢
WelcomeMessage=Welcome Mr. {firstName} {lastName} !!!
These firstName and LastName variable needs to be substituted dynamically. Should we use velocity template engine for the same? Or are there any other opensource frameworks for the same?
这些 firstName 和 LastName 变量需要动态替换。我们应该使用速度模板引擎吗?或者还有其他相同的开源框架吗?
Thanks, Manish
谢谢,曼尼什
回答by Joseph Kulandai
velocity is the best tool as of now. But it depends on what file type you want to use as a template.
速度是目前最好的工具。但这取决于您要用作模板的文件类型。
For example, if you want to use MS word docs as template, then you have to extend velocity classess and write your own implementation.
例如,如果您想使用 MS Word 文档作为模板,那么您必须扩展速度类并编写自己的实现。
回答by Chandra Sekar
You can use the MessageFormatclass of Java SE. It allows you to do exactly what you ask for.
您可以使用Java SE的MessageFormat类。它可以让您完全按照您的要求去做。
In your case the below code snippet must do the trick, assuming props contains all the properties loaded from your file.
在您的情况下,以下代码片段必须起作用,假设 props 包含从您的文件加载的所有属性。
MessageFormat.format((String) props.get("WelcomeMessage"), "First", "Last");
Note that your properties files should have index of parameters instead of named parameters as below.
请注意,您的属性文件应该具有参数索引而不是命名参数,如下所示。
WelcomeMessage=Welcome Mr. {0} {1} !!!
回答by skaffman
Velocity is rather old and unpleasant, in my opinion, there are nicer ways to do this:
Velocity 相当陈旧且令人不快,在我看来,有更好的方法来做到这一点:
- StringTemplateis the simplest of the template engines, and good enough for what you need (see syntax examples here).
- If you're already using Spring 3, it has the PropertyPlaceholderHelperclass which can do this also, but I wouldn't use Spring just to get hold of this one class.
- StringTemplate是最简单的模板引擎,足以满足您的需求(请参阅此处的语法示例)。
- 如果您已经在使用 Spring 3,它有PropertyPlaceholderHelper类,它也可以做到这一点,但我不会使用 Spring 来获取这一类。
回答by Benny Neugebauer
In a Java web application with JSF 2 that would work as follows:
在具有 JSF 2 的 Java Web 应用程序中,其工作方式如下:
src\main\webapp\WEB-INF\faces-config.xml
src\main\webapp\WEB-INF\faces-config.xml
...
<resource-bundle>
<base-name>com.mycompany.resources.messages</base-name>
<var>mytext</var>
</resource-bundle>
...
src\main\resources\com\mycompany\resources\messages\mytext.properties
src\main\resources\com\mycompany\resources\messages\mytext.properties
WelcomeMessage = Welcome Mr. {0} {1} !!!
index.xhtml
索引.xhtml
<h:outputFormat value="#{mytext.WelcomeMessage}" >
<f:param value="#{userSessionBean.first}" />
<f:param value="#{userSessionBean.last}" />
</h:outputFormat>
回答by dpkcs
One of the way is string substitutor:
其中一种方法是字符串替换器:
WelcomeMessage=Welcome Mr. ${firstName} ${lastName} !!!
Map<String, String> valuesMap = new HashMap<String, String>();
valuesMap.put("firstName", "ram");
valuesMap.put("lastName", "Kumar");
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String message = sub.replace(WelcomeMessage);
回答by user7294900
Another option is adding Apache FreeMarkerwith no dependencies and define template as:
另一种选择是添加没有依赖项的Apache FreeMarker并将模板定义为:
Welcome Mr. ${firstName} ${lastName} !!!
Apache FreeMarker? is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language
Apache FreeMarker?是一个模板引擎:一个 Java 库,用于根据模板和变化的数据生成文本输出(HTML 网页、电子邮件、配置文件、源代码等)。模板是用 FreeMarker 模板语言 (FTL) 编写的,这是一种简单的专用语言
You can use StringTemplateLoaderto load template using String
您可以使用StringTemplateLoader使用 String 加载模板
you can create a StringTemplateLoader and add each template to it:
您可以创建一个 StringTemplateLoader 并将每个模板添加到其中: