简体   繁体   English

velocity模板转换为java字符串

[英]velocity template convert into a java string

I want to use Java Mail to send an email but I want to define the content and structure of the mail using a velocity template. 我想使用Java Mail发送电子邮件,但我想使用速度模板定义邮件的内容和结构。 Is there any way to make a conversion of the velocity template that yields a String in the way I could use it like this: 有没有办法转换速度模板,产生一个String,就像我可以这样使用它:

String html = velocityConversion_or_whatever();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(html, "text/html");

Thanks. 谢谢。

Velocity can render a template into a StringWriter , so from there it's pretty straightforward to grab the HTML out of your string. Velocity可以将模板呈现为StringWriter ,因此从那里获取字符串中的HTML非常简单。 Assuming you've already configured your Velocity engine you can do this: 假设您已经配置了Velocity引擎,您可以这样做:

Map data = new HashMap();
// Add data to your map here

StringWriter writer = new StringWriter();
VelocityContext velocityContext = new VelocityContext(data);
velocityEngine.mergeTemplate(templateLocation, velocityContext, writer);
String html = writer.toString();

Velocy有这样的util类:

VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateUrl, model);

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

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