Given a dictionary:
1=f00
2=bar
3=larodi
.
.
.
and some html files like:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
content content content {1} b开发者_运维知识库la
bla bla {3} &%$*#$*$%# {2} and so on...
</body>
</html>
How to replace the keys in the files with their values with java? I've read about java.text.MessageFormat, but cannot come up with a way to use it.
String result = MessageFormat.format(htmlString, "foo", "bar", "larodi");
String[] paramas = {"foo" , ....};
String result = MessageFormat.format(htmlString, params);
htmlString is your html content.
You may want to check some java template engines like freemarker and velocity.
It seems to me that a message formatter does not use dictionaries, but arrays. Your '1' is, as such, not a key but an index.
It looks a bit like you remember the python %
operator. This isn't like that. MessageFormat uses a positional system. Either by entering the parameters directly, or by giving an array.
精彩评论