如何评估这种参数或我需要传递JSON?我可以改变结构的参数:"news[0].title"
或“news.0.title"
或其他任何东西,但我不喜欢问我的API的用户形成JSONFreemarker - 传递参数的平面结构,传递到对象数组
@Autowired
private TemplateEmailBodyPreparer preparer;
public void doIt() {
Map<String,String> properties = new HashMap<String,String>() {{
put("news[0].title", "Title 1");
put("news[0].body", "Body 1");
put("news[1].title", "Title 2");
put("news[1].body", "Body 2");
}};
String result = preparer.getByTemplate("mail/html/news.ftl", properties);
System.out.println("Result = " + result);
}
@Service
public class TemplateEmailBodyPreparer implements EmailBodyPreparer {
@Autowired
private Configuration freeMarkerConfiguration;
public String getByTemplate(String templatePath, Map<String,String> properties) {
try {
Template template = freeMarkerConfiguration.getTemplate(templatePath, "UTF-8");
return FreeMarkerTemplateUtils.processTemplateIntoString(template, properties);
} catch (IOException | TemplateException e) {
throw new IllegalArgumentException("Unable to build template: " + e.getMessage());
}
}
}
邮件/ HTML/news.ftl
<!DOCTYPE html>
<html>
<head></head>
<body>
<#list news as content>
${content.title} - ${content.body}
</#list>
</body>
</html>
。
错误:
Caused by: java.lang.IllegalArgumentException: Unable to build template: The following has evaluated to null or missing:
==> news [in template "mail/html/news.ftl" at line 5, column 11]
什么是.0'-s和'.1'-s?这是地图列表吗? – ddekany 2014-11-04 07:16:24
@ddekany,这是数组的索引。请允许我添加一些细节。查看更新的帖子。 – Ivan 2014-11-05 12:29:07
尽管在FreeMarker中没有这样的东西,但它是'$ {emails.0.body}'。它是'$ {emails [0] .body}'。但是对于迭代,通常使用'<#emails email as email> ... $ {email.body} ...#list>'。你卡在哪里? – ddekany 2014-11-05 20:03:09