背景
为了实现一个根据选择不同的模板进模板内容的替换
新建一个Content.java
类,他包含了一个根据模板生成对应的字符窜内容的方法
package com.drink.admin.print.yly;
import com.drink.admin.print.TemplateData;
public interface Content {
String generateContentByTemplate(TemplateData templateData);
}
新建一个模板的实现类 OneContent.java
类
package com.drink.admin.print.yly;
import com.drink.admin.print.TemplateData;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class OneContent implements Content {
@Override
public String generateContentByTemplate(TemplateData templateData) {
String ticketTemplateOne = "<FB><FW><FH><FS><center>${template.storeName}</center></FS></FH></FW></FB>\n" +
"<FB><FW><FH><FS><center>#${template.orderTakeNo}</center></FS></FH></FW></FB>" + "\n" +
"<FB><center>${template.storeAddress}</center></FB>\r";
ticketTemplateOne += "<FB><FH><center>餐桌号:${template.orderTableCode}</center></FH></FB>";
ticketTemplateOne +=
"<FB>订单号:${template.orderNo}\r下单时间:${template.orderTime}</FB>\n" +
"<center> ———————————————</center>" + "\r" +
"<FB>收货人:</FB>${template.receiveName}\r\r" +
"<FB>收货地址:</FB>${template.receiveAddress}\r\r" +
"<FB>电话号码:</FB>${template.receivePhone}\r\r" +
"<center>…………………………………………</center>" + "\r${template.itemContent}\n\n" +
"<center>…………………………………………</center>" +
"<table><tr><td>包装费</td><td></td><td>¥${template.packFee}</td></tr></table>\r" +
"<table><tr><td>配送费</td><td></td><td>¥${template.shipFee}</td></tr></table>\r" +
"<table><tr><td>优惠券及活动</td><td></td><td>-¥${template.activityCouponPrice}</td></tr></table>\r" +
"<table><tr><td>合计</td><td>${template.countNumber}</td><td>¥${template.realPayPrice}</td></tr></table>\r" +
"备注: ${template.memo}\r" +
"<center>…………………………………………</center>" +
"<FB>实付金额</FB><table><tr><td></td><td>¥${template.realPayPrice}</td></tr></table>" +
"<center>…………………………………………</center>" +
"<QR>${template.kfQrcode}</QR>\r" +
"<FB><FS><center>如有问题请联系客服</center></FS></FB><center>${template.storeTel}</center>";
StringWriter result = new StringWriter();
Map templateMap = new HashMap();
templateMap.put("template", templateData);
Template template = null;
try {
template = new Template("strTpl", ticketTemplateOne, new Configuration(new Version("2.3.23")));
template.process(templateMap, result);
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
}
}
新建一个枚举类,包含了所有的模板相关的信息PrintTemplateEnum.java
package com.drink.admin.print.yly.constant;
import com.drink.admin.print.yly.model.TemplateOneData;
import com.drink.admin.print.yly.model.TemplateTwoData;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import static com.drink.admin.print.yly.constant.YlyTemplate.*;
/**
* @author xu
*/
public enum PrintTemplateEnum {
/**
* 模板一 打印小票
*/
TEMPLATE_ONE(1, new OneContent());
private Integer code;
private Content content;
PrintTemplateEnum(int code, Content content) {
this.code = code;
this.content = content;
}
public Integer getCode() {
return code;
}
public Content getContent() {
return content;
}
}
测试调用
public static void main(String[] args) {
TemplateData templateData = new TemplateData();
// 选中对应的内容
for (PrintTemplateEnum value : PrintTemplateEnum.values()) {
if (value.getCode().equals(1)) {
Content content = value.getContent();
String s = content.generateContentByTemplate(templateData);
System.out.println(s);
}
}
}