简体   繁体   English

替换HTML文件中的模板

[英]Replace templates in html files

Given a dictionary: 给定字典:

1=f00
2=bar
3=larodi
.
.
.

and some html files like: 和一些html文件,例如:

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>  
   content content content {1} bla
   bla bla {3} &%$*#$*$%# {2} and so on...
</body>
</html>

How to replace the keys in the files with their values with java? 如何用Java将文件中的键替换为其值? I've read about java.text.MessageFormat, but cannot come up with a way to use it. 我已经阅读了有关java.text.MessageFormat的内容,但是无法提出一种使用它的方法。

String result = MessageFormat.format(htmlString, "foo", "bar", "larodi");


String[] paramas = {"foo" , ....};
String result = MessageFormat.format(htmlString, params);

htmlString is your html content. htmlString是您的html内容。

您可能需要检查一些Java模板引擎,例如freemarkervelocity

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. 因此,您的“ 1”不是键,而是索引。

It looks a bit like you remember the python % operator. 看起来有点像您还记得python %运算符。 This isn't like that. 不是那样的 MessageFormat uses a positional system. MessageFormat使用位置系统。 Either by entering the parameters directly, or by giving an array. 通过直接输入参数或给定数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM