如何在 java 中留小胡子 html
How to get mustache html in java
我正在尝试使用 mustache 为我填充一个 html,然后我想得到那个 html 并将其用作字符串。
这样的模板 -> template.xhtml
<table style="width:10%" align="center">
<tr>
<th>Old</th>
<th>New</th>
</tr>
<tr>
<td>{{old}}</td>
<td>{{new}}</td>
</tr>
</table>
像这样的哈希:
HashMap<String, String> scopes = new HashMap<String, String>();
scopes.put("old", "Testing");
scopes.put("new", "Mustache");
现在我怎么告诉小胡子使用 template.xhtml 并填充范围,然后 return 我 html?
查看 Mustache 项目自述文件的最底部 (https://github.com/spullara/mustache.java#readme)。那里有一个示例 main
方法几乎可以满足您的需要。只需使用 StringWriter
而不是 OutputStreamWriter
,这样您就可以获得结果 HTML 作为 String
.
String aux = "";
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("path/to/file.html");
StringWriter stringWriter = new StringWriter();
mustache.execute(stringWriter, wrapper);
aux = stringWriter.toString();
System.out.println(aux);
我在 Android 中遇到了同样的问题,我将 html 解析为字符串:
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
buf.flush();
}
return buf.toString();
}
在数据中填写模板:
Map<String, String> data = new HashMap<>();
data.put("data", "123");
data.put("read_mode","day");
data.put("font_size","small");
Template tmpl = Mustache.compiler().compile(readInputStreamAsString(***InputStream)));
String html = tmpl.execute(data);
工作正常。
我正在使用 spring 初学者小胡子。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader("emailTemplates/", ".html"); // Inside resources emailTemplates dir
Reader reader = loader.getTemplate("index");
Template tmpl = Mustache.compiler().compile(reader);
// Template String "One, two, {{three}}. Three sir!"
Map<String, String> data = new HashMap<String, String>();
data.put("three", "six");
希望对您有所帮助。
我正在尝试使用 mustache 为我填充一个 html,然后我想得到那个 html 并将其用作字符串。
这样的模板 -> template.xhtml
<table style="width:10%" align="center">
<tr>
<th>Old</th>
<th>New</th>
</tr>
<tr>
<td>{{old}}</td>
<td>{{new}}</td>
</tr>
</table>
像这样的哈希:
HashMap<String, String> scopes = new HashMap<String, String>();
scopes.put("old", "Testing");
scopes.put("new", "Mustache");
现在我怎么告诉小胡子使用 template.xhtml 并填充范围,然后 return 我 html?
查看 Mustache 项目自述文件的最底部 (https://github.com/spullara/mustache.java#readme)。那里有一个示例 main
方法几乎可以满足您的需要。只需使用 StringWriter
而不是 OutputStreamWriter
,这样您就可以获得结果 HTML 作为 String
.
String aux = "";
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("path/to/file.html");
StringWriter stringWriter = new StringWriter();
mustache.execute(stringWriter, wrapper);
aux = stringWriter.toString();
System.out.println(aux);
我在 Android 中遇到了同样的问题,我将 html 解析为字符串:
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
buf.flush();
}
return buf.toString();
}
在数据中填写模板:
Map<String, String> data = new HashMap<>();
data.put("data", "123");
data.put("read_mode","day");
data.put("font_size","small");
Template tmpl = Mustache.compiler().compile(readInputStreamAsString(***InputStream)));
String html = tmpl.execute(data);
工作正常。
我正在使用 spring 初学者小胡子。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader("emailTemplates/", ".html"); // Inside resources emailTemplates dir
Reader reader = loader.getTemplate("index");
Template tmpl = Mustache.compiler().compile(reader);
// Template String "One, two, {{three}}. Three sir!"
Map<String, String> data = new HashMap<String, String>();
data.put("three", "six");
希望对您有所帮助。