
正文
JAVA FreeMarker工具类
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示

FreeMarkerUtil.java
package pers.kangxu.datautils.utils; import java.io.File;
import java.io.StringWriter;
import java.util.Map; import pers.kangxu.datautils.common.Constants; import freemarker.template.Configuration;
import freemarker.template.Template; /**
*
* <b>
* FreeMarkerUtil 模板工具
* </b>
* @author kangxu
*
*/
public class FreeMarkerUtil { /**
*
* TODO
* <br>
* @author kangxu2 2016-11-23
*
* @param fltFile flt文件名
* @param templatePath flt文件路径 src/template
* @param datas 数据集合
* @return
*/
public static String geneFileStr(String fltFile,String templatePath,Map<String, Object> datas){ Configuration cfg = new Configuration();
try {
cfg.setDirectoryForTemplateLoading(new File(templatePath));
Template template = cfg.getTemplate(fltFile,Constants.ENCODING);
template.setEncoding(Constants.ENCODING); StringWriter out = new StringWriter(); template.process(datas, out); out.flush();
out.close(); return out.getBuffer().toString(); } catch (Exception e) {
e.printStackTrace();
} return null; } }
使用方式 FreeMarkerUtilTester.java
package pers.kangxu.datautils.test; import java.util.HashMap;
import java.util.Map; import pers.kangxu.datautils.common.verifycode.BuildVerifyCode;
import pers.kangxu.datautils.utils.FreeMarkerUtil; public class FreeMarkerUtilTester { public static void main(String[] args) {
Map<String, Object> datas = new HashMap<String, Object>();
datas.put("code", BuildVerifyCode.generateVerifyCode(4));
System.out.println(FreeMarkerUtil.geneFileStr("sms.flt","template",datas));
}
}
sms.flt
您的验证码是:${code}。请不要把验证码泄露给其他人。
运行结果







