Dynamic Placeholder substitution in properties in java
Asked Answered
M

6

25

I wanted to substitute the placeholder dynamically in properties in a java application. Like

 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?

Thanks, Manish

Marutani answered 5/2, 2010 at 7:29 Comment(0)
B
53

You can use the MessageFormat class of Java SE. It allows you to do exactly what you ask for.

In your case the below code snippet must do the trick, assuming props contains all the properties loaded from your file.

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} !!!
Bibliophage answered 5/2, 2010 at 7:38 Comment(2)
It does, yes, except that inexplicably, it's defined using numeric placeholders rather than meaningful symbolic ones. Amazing how much harder that is to use.Zeniazenith
Edited my reply just as you were commenting. :)Bibliophage
B
6

Velocity is rather old and unpleasant, in my opinion, there are nicer ways to do this:

  • StringTemplate is 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 PropertyPlaceholderHelper class which can do this also, but I wouldn't use Spring just to get hold of this one class.
Battologize answered 5/2, 2010 at 8:24 Comment(1)
Velocity is not perfect. However I think it does its job very well and I can recommend it very much. Furthermoe the toolbox concept allows very easy integration of your own "templating logic". And it has a great integration with Servlets in general (and Struts 1.x)Haggle
R
6

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);
Rapport answered 13/6, 2017 at 4:58 Comment(1)
The most recent version of this class is StringSubstitutor in org.apache.commons.commons-textAgog
O
2

Another option is adding Apache FreeMarker with no dependencies and define template as:

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

You can use StringTemplateLoader to load template using String

you can create a StringTemplateLoader and add each template to it:

Oneman answered 4/9, 2018 at 11:51 Comment(0)
C
0

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.

Cruller answered 5/2, 2010 at 7:34 Comment(1)
too heavy, too complicatedLedesma
A
0

In a Java web application with JSF 2 that would work as follows:

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

WelcomeMessage = Welcome Mr. {0} {1} !!!

index.xhtml

<h:outputFormat value="#{mytext.WelcomeMessage}" >          
  <f:param value="#{userSessionBean.first}" />
  <f:param value="#{userSessionBean.last}" />
</h:outputFormat>
Appaloosa answered 3/5, 2014 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.