Freemarker似乎沒有方便的內置模板生成隨機UUID。Freemarker:方便地生成UUID
我能想到的最好方法是創建一個Freemarker method;在我的Java 8代碼中,我將該方法注入模板中以備後用。例如:
public String generate(Map<String, Object> data, String templateLocation) throws IOException, TemplateException {
try (StringWriter writer = new StringWriter()) {
Template template = configuration.getTemplate(templateLocation);
// UUID generation method injected in this line:
data.put("uuid", (TemplateMethodModelEx) (list) -> UUID.randomUUID());
template.process(data, writer);
return writer.toString();
}
}
在Freemarker模板然後我就可以用這樣的方法:
${uuid()}
有沒有更方便的解決方案產生了Freemarker的UUID?
這裏是我的Maven的依賴關係:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.25-incubating</version>
<scope>test</scope>
</dependency>