然后写一个加载ftl文件的Java类
import java.io.StringWriter;
import java.util.Map;
import com.service.impl.MonServiceImpl;
import freemarker.template.Configuration;
import freemarker.template.Template;
/*
* use freemarker to send email
*/
public class FreemarkUtil {
private static Configuration cfg;
private static void initialConfig() {
cfg = new Configuration();
cfg.setClassForTemplateLoading(MonServiceImpl.class,
"/package的名字,用'/'隔开/");
}
/*
* templateName means the ftl file name
* param means the Object used in ftl
*/
public static String getMail(String templateName,
Map<String, Object> param) {
String result = null;
try {
if (cfg == null) {
initialConfig();
}
Template temp = cfg.getTemplate(templateName);
StringWriter writer = new StringWriter();
temp.process(param, writer);
result = writer.toString();
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
然后在需要网ftl中输出内容的地方
paramMap.put("AM", a); AM即FTL中的对象名,要一致
String mailContent = FreemarkUtil.getMail("FTL文件的名字.ftl",
paramMap);
mail.setContent(mailContent);
通过以上语句就可以是实现了。
