1.Freemarker
public void message(String mark, String type, Orderform orderform, String userId, String shouhouleixing) {
if (shouhouleixing.equals("0")) {
shouhouleixing = "仅退款";
} else if (shouhouleixing.equals("1")) {
shouhouleixing = "退货退款";
} else if (shouhouleixing.equals("2")) {
shouhouleixing = "退货退款";
}
Map<String, Object> map = new HashMap<>();
StringWriter writer = new StringWriter();
try {
Template templates = this.templateService.getByMark(mark);
// File file=new File("./src/main/resources/templates/freemarker/index.ftl");
String path1 = this.getClass().getClassLoader().getResource("").getPath() + "templates/freemarker/index.ftl";
System.out.println(path1);
PrintWriter pwrite = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path1)));
pwrite.print(templates.getContent());
pwrite.flush();
pwrite.close();
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
//加载模板
ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), "/templates/freemarker");
//cfg.setDirectoryForTemplateLoading(new File("./src/main/resources/templates/freemarker"));
TemplateLoader[] loaders = new TemplateLoader[]{ctl};
MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
cfg.setTemplateLoader(mtl);
cfg.setDateFormat("yyyy-MM-dd HH:mm:ss");
cfg.setEncoding(Locale.CHINA, "utf-8");
cfg.setDefaultEncoding("utf-8");
freeMarkerConfigurer.setConfiguration(cfg);
Map<String, Object> context = new HashMap<>();
context.put("send_time", new Date());
context.put("shouhouleixing", shouhouleixing);
context.put("orderForm", orderform);
//获取模板
Template template = cfg.getTemplate("index.ftl", "utf-8");
template.process(context, writer);
String content = writer.toString();
String userName = "admin";
IUser admin = this.userService.getUserByUserName(userName);
Message msg = new Message();
msg.setAddTime(new Date());
msg.setContent(content);
msg.setFromUserId(admin.getId());
msg.setTitle(templates.getTitle());
msg.setToUserId(Long.parseLong(userId));
msg.setDeleteStatus(false);
msg.setActivityType(0);
msg.setStatus(0);
msg.setReplyStatus(0);
msg.setImportantStatus(false);
if (type.equals("4")) {
msg.setType(4);
} else if (type.equals("5")) {
msg.setType(5);
}
msg.setAuditStatus(1);
msg.setActId(0);
Integer save = this.messageService.save(msg);
if (save > 0) {
map.clear();
map.put("message", "发送成功");
}
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}
2.Velocity
private void launchedAfterSale_template(String mark,TuiKuanShouHou tuiKuanShouHou,String type) throws Exception {
Template template = this.templateService.getObjByProperty("mark", mark);
if (template.isOpen()){
String path = System.getProperty("********") + "vm" + File.separator;
PrintWriter pwrite = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(path + "msg.vm", false), "UTF-8"));
pwrite.print(template.getContent());
pwrite.flush();
pwrite.close();
Properties p = new Properties();
p.setProperty("file.resource.loader.path", System.getProperty("****") + "vm" + File.separator);
p.setProperty("input.encoding", "UTF-8");
p.setProperty("output.encoding", "UTF-8");
Velocity.init(p);
org.apache.velocity.Template blank = Velocity.getTemplate("msg.vm", "UTF-8");
VelocityContext context = new VelocityContext();
context.put("tuiKuanShouHou", tuiKuanShouHou);
context.put("config", this.configService.getSysConfig());
context.put("send_time", CommUtil.formatLongDate(new Date()));
context.put("webPath", this.configService.getSysConfig().getAddress());
StringWriter writer = new StringWriter();
blank.merge(context, writer);
String content = writer.toString();
User admin = this.userService.getObjByProperty("userName", "admin");
Message msg = new Message();
msg.setAddTime(new Date());
msg.setContent(content);
msg.setFromUser(admin);
msg.setTitle(template.getTitle());
msg.setToUser(tuiKuanShouHou.getOrderForm().getStore().getUser());
if (type.equals("4")){
msg.setType(4);
}else if (type.equals("5")){
msg.setType(5);
}
msg.setAuditStatus(1);
this.messageService.save(msg);
CommUtil.deleteFile(path + "msg.vm");
writer.flush();
writer.close();
}
}
orderForm
${(send_time?string("yyyy-MM-dd HH:mm:ss"))!}